1 /*
   2  * Copyright (c) 2012, 2014, 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 Test7116786
  26  * @summary verify that VerifyError messages are as expected
  27  * @library testcases.jar
  28  * @run main/othervm -Xverify:all Test7116786
  29  */
  30 
  31 
  32 /**
  33  * This class contains information regarding when a VerifyError is thrown
  34  * in the verifier.  Most of the data is informational-only, and can be
  35  * used to track down where and why VerifyErrors are thrown.  As such it
  36  * is possible the information may go out-of-date.
  37  *
  38  * The only fields used for the purpose of testing is the 'caseName' and
  39  * the 'message'.  The 'caseName' corresponds to a classfile which exhibits
  40  * the VerifyError, and the 'message' is a regular expression which we expect
  41  * to match the verify error message.  If the 'message' doesn't match what
  42  * we expect, it warrents investigation to see if we are still triggering
  43  * the VerifyError that we expect.  It could simply just be that the message
  44  * changed, which is fine.
  45  *
  46  * Some cases are not testable, either because the code is probably unreachable
  47  * or the test classfile would be too onerous to create.  These cases are
  48  * marked with 'testable' == false, and the test runner will skip them.
  49  */
  50 class Case {
  51     private String caseName;    // Name of the case
  52     private String file;        // Source file where VerifyError is thrown
  53     private String location;    // enclosing function or switch case
  54     private String description; // What causes this VerifyError
  55     private String message;     // The VerifyError message used.
  56 
  57     private boolean testable;   // Whether this case is testable or not.
  58 
  59     public Case(String caseName, String file, boolean testable,
  60                 String location, String description, String message) {
  61         this.caseName = caseName;
  62         this.file = file;
  63         this.testable = testable;
  64         this.location = location;
  65         this.description = description;
  66         this.message = message;
  67     }
  68 
  69     String getCaseName() { return this.caseName; }
  70     String getFile() { return this.file; }
  71     String getLocation() { return this.location; }
  72     String getDescription() { return this.description; }
  73     String getMessage() { return this.message; }
  74 
  75     boolean isTestable() { return this.testable; }
  76 }
  77 
  78 /**
  79  * These are the locations in the source code where VerifyErrors are thrown
  80  * as of today, 2012/07/18.  These may change as the verification code is
  81  * modified, which is ok.  This test is trying to provide coverage for all
  82  * VerifyErrors (just to make sure there are no crashes) and it's probably
  83  * not necessary to update it every time the VM changes.
  84  */
  85 class VerifyErrorCases {
  86     public static final Case[] cases = {
  87 
  88         new Case("case00", "stackMapFrame.cpp", true, "pop_stack_ex",
  89                  "stack underflow",
  90                  "Operand stack underflow"),
  91 
  92         new Case("case01", "stackMapFrame.cpp", true, "pop_stack_ex",
  93                  "stack pop not assignable to expected",
  94                  "Bad type on operand stack"),
  95 
  96         new Case("case02", "stackMapFrame.cpp", true, "get_local",
  97                  "local index out-of-bounds",
  98                  "Local variable table overflow"),
  99 
 100         new Case("case03", "stackMapFrame.cpp", true, "get_local",
 101                  "local not assignable to expected",
 102                  "Bad local variable type"),
 103 
 104         new Case("case04", "stackMapFrame.cpp", true, "get_local_2",
 105                  "local index out-of-bounds [type2]",
 106                  "get long/double overflows locals"),
 107 
 108         new Case("case05", "stackMapFrame.cpp", true, "get_local_2",
 109                  "local not assignabled to expected [type2]",
 110                  "Bad local variable type"),
 111 
 112         /* Unreachable: Can't split long/double on stack */
 113         new Case("case06", "stackMapFrame.cpp", false, "get_local_2",
 114                  "local second-word not assignabled to expected",
 115                  "Bad local variable type"),
 116 
 117         new Case("case07", "stackMapFrame.cpp", true, "set_local",
 118                  "local index out-of-bounds",
 119                  "Local variable table overflow"),
 120 
 121         new Case("case08", "stackMapFrame.cpp", true, "set_local_2",
 122                  "local index out-of-bounds [type2]",
 123                  "Local variable table overflow"),
 124 
 125         new Case("case09", "stackMapFrame.hpp", true, "push_stack",
 126                  "stack overflow",
 127                  "Operand stack overflow"),
 128 
 129         new Case("case10", "stackMapFrame.hpp", true, "push_stack_2",
 130                  "stack overflow [type2]",
 131                  "Operand stack overflow"),
 132 
 133         new Case("case11", "stackMapFrame.hpp", true, "pop_stack",
 134                  "stack underflow",
 135                  "Operand stack underflow"),
 136 
 137         new Case("case12", "stackMapTable.cpp", true, "StackMapTable ctor",
 138                  "stackmap offset beyond code size",
 139                  "StackMapTable error: bad offset"),
 140 
 141         new Case("case13", "stackMapTable.cpp", true, "match_stackmap",
 142                  "no stackmap frame at expected location",
 143                  "Expecting a stackmap frame at branch target "),
 144 
 145         new Case("case14", "stackMapTable.cpp", true, "check_jump_target",
 146                  "no stackmap frame at jump location or bad jump",
 147                  "Inconsistent stackmap frames at branch target "),
 148 
 149         /* Backward jump with uninit is allowed starting with JDK 8 */
 150         new Case("case15", "stackMapTable.cpp", false, "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         /* No longer a valid test case for bytecode version >= 51. Nonzero
 342          * padding bytes are permitted with lookupswitch and tableswitch
 343          * bytecodes as of JVMS 3d edition */
 344         new Case("case58", "verifier.cpp", false, "verify_switch",
 345                  "bad switch padding",
 346                  "Nonzero padding byte in lookupswitch or tableswitch"),
 347 
 348         new Case("case59", "verifier.cpp", true, "verify_switch",
 349                  "tableswitch low is greater than high",
 350                  "low must be less than or equal to high in tableswitch"),
 351 
 352         /* Unreachable on 64-bit?  Only way to get here is to overflow
 353          * the 'keys' variable which can't happen on 64-bit since we're dealing
 354          * with 32-bit values.  Perhaps reachable on 32-bit but the
 355          * triggering class would be quite large */
 356         new Case("case60", "verifier.cpp", false, "verify_switch",
 357                  "high - low + 1 < 0 (overflow?)",
 358                  "too many keys in tableswitch"),
 359 
 360         /* Would have to create a 16G classfile to trip this.  Possible but
 361          * not reasonable to do in a test.  */
 362         new Case("case61", "verifier.cpp", false, "verify_switch",
 363                  "lookupswitch keys < 0",
 364                  "number of keys in lookupswitch less than 0"),
 365 
 366         new Case("case62", "verifier.cpp", true, "verify_switch",
 367                  "lookupswitch keys out-of-order",
 368                  "Bad lookupswitch instruction"),
 369 
 370         /* Unreachable: Class file parser verifies Fieldref contents */
 371         new Case("case63", "verifier.cpp", false, "verify_field_instructions",
 372                  "referenced class is not an CP object",
 373                  "Expecting reference to class in class "),
 374 
 375         new Case("case64", "verifier.cpp", true, "verify_field_instructions",
 376                  "TOS not assignable to field type in putfield",
 377                  "Bad type on operand stack in putfield"),
 378 
 379         new Case("case65", "verifier.cpp", true, "verify_field_instructions",
 380                  "TOS not assignable to class when accessing protected field",
 381                  "Bad access to protected data in getfield"),
 382 
 383         new Case("case66", "verifier.cpp", true, "verify_invoke_init",
 384                  "Uninit_this is not of the current type or it's supertype",
 385                  "Bad <init> method call"),
 386 
 387         /* Unreachable:  Stack map parsing ensures valid type and new
 388          * instructions have a valid BCI. */
 389         new Case("case67", "verifier.cpp", false, "verify_invoke_init",
 390                  "Uninit type with bad new instruction index",
 391                  "Expecting new instruction"),
 392 
 393         new Case("case68", "verifier.cpp", true, "verify_invoke_init",
 394                  "calling other class's <init> method",
 395                  "Call to wrong <init> method"),
 396 
 397         new Case("case69", "verifier.cpp", true, "verify_invoke_init",
 398                  "Calling protected <init> and type unassignable from current",
 399                  "Bad access to protected <init> method"),
 400 
 401         new Case("case70", "verifier.cpp", true, "verify_invoke_init",
 402                  "TOS is not an uninitialized (or Uninit_this) type",
 403                  "Bad operand type when invoking <init>"),
 404 
 405         new Case("case71", "verifier.cpp", true, "verify_invoke_instructions",
 406                  "Arg count in instruction doesn't match signature",
 407                  "Inconsistent args count operand in invokeinterface"),
 408 
 409         new Case("case72", "verifier.cpp", true, "verify_invoke_instructions",
 410                  "Non-zero pad in invokeinterface",
 411                  "Fourth operand byte of invokeinterface must be zero"),
 412 
 413         new Case("case73", "verifier.cpp", true, "verify_invoke_instructions",
 414                  "Non-zero pad in invokedynamic",
 415                  "Third and fourth operand bytes of " +
 416                  "invokedynamic must be zero"),
 417 
 418         new Case("case74", "verifier.cpp", true, "verify_invoke_instructions",
 419                  "Non-invokespecial trying to invoke a '<' method",
 420                  "Illegal call to internal method"),
 421 
 422         new Case("case75", "verifier.cpp", true, "verify_invoke_instructions",
 423                  "invokespecial and current unassignable from referenced type",
 424                  "Bad invokespecial instruction: current class isn't " +
 425                  "assignable to reference class."),
 426 
 427         new Case("case76", "verifier.cpp", true, "verify_invoke_instructions",
 428                  "TOS not assignable to current when calling protected method",
 429                  "Bad access to protected data in invokevirtual"),
 430 
 431         /* Unreachable:  class file parser enforces void signature */
 432         new Case("case77", "verifier.cpp", false, "verify_invoke_instructions",
 433                  "<init> method is not void return",
 434                  "Return type must be void in <init> method"),
 435 
 436         new Case("case78", "verifier.cpp", true, "get_newarray_type",
 437                  "newarray type invalid",
 438                  "Illegal newarray instruction"),
 439 
 440         new Case("case79", "verifier.cpp", true, "verify_return_value",
 441                  "void return from method which has a return value",
 442                  "Method expects a return value"),
 443 
 444         new Case("case80", "verifier.cpp", true, "verify_return_value",
 445                  "TOS type does not match signature",
 446                  "Bad return type"),
 447 
 448         new Case("case81", "verifier.cpp", true, "verify_stackmap_table",
 449                  "stack map does not match (flags)",
 450                  "Instruction type does not match stack map")
 451     };
 452 }
 453 
 454 public class Test7116786 {
 455     public static void main(String argv[]) throws Exception {
 456         for (Case c : VerifyErrorCases.cases) {
 457             System.out.println("******** " + c.getCaseName() + " ********");
 458             if (c.isTestable()) {
 459                 try {
 460                     ClassLoader cl = Test7116786.class.getClassLoader();
 461                     Class<?> cls = Class.forName(c.getCaseName(), true, cl);
 462                     throw new RuntimeException(
 463                         "++ FAIL: No verify error encountered");
 464                 } catch (VerifyError ve) {
 465                     String message = c.getMessage();
 466                     String veMessage = ve.getMessage();
 467                     System.out.print(veMessage);
 468                     if (!veMessage.startsWith(message)) {
 469                         // We're not seeing the message we expect.  Could be
 470                         // that we've gotten the wrong VerifyError case, or
 471                         // maybe the message changed.
 472                         System.out.println("++ FAIL? " +
 473                             "Message does not match what was expected: " +
 474                             message);
 475                         continue;
 476                     }
 477                     if (!veMessage.contains("Exception Details:") &&
 478                         !veMessage.contains("Reason:")) {
 479                         System.out.println("++ FAIL: No details found");
 480                         throw new RuntimeException("FAIL: No details found");
 481                     }
 482                     System.out.println("++ PASS");
 483                 }
 484             } else {
 485                System.out.println("++ SKIPPED");
 486             }
 487         }
 488     }
 489 }