1 /*
   2  * Copyright (c) 2017, 2020, 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 8046171 8211065
  27  * @summary Setup nestmate calls to private methods then use
  28  *          modified jcod classes to introduce errors. Test with
  29  *          and without verification enabled
  30  * @compile TestInvokeErrors.java
  31  * @compile MissingMethod.jcod
  32  *          MissingMethodWithSuper.jcod
  33  *          MissingNestHost.jcod
  34  * @run main TestInvokeErrors true
  35  * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:-BytecodeVerificationRemote -XX:-BytecodeVerificationLocal TestInvokeErrors false
  36  */
  37 
  38 public class TestInvokeErrors {
  39 
  40     static class Nested {
  41         private void priv_invoke() {
  42             System.out.println("Nested::priv_invoke");
  43         }
  44     }
  45 
  46     static class MissingMethod {
  47         // jcod version will rename this method to not_priv_invoke
  48         private void priv_invoke() {
  49             System.out.println("MissingMethod::priv_invoke");
  50         }
  51     }
  52 
  53     static class MissingMethodWithSuper extends Nested {
  54         // jcod version will rename this method to not_priv_invoke
  55         private void priv_invoke() {
  56             System.out.println("MissingMethodWithSuper::priv_invoke");
  57         }
  58     }
  59 
  60     static class MissingNestHost {
  61         // jcod version will change NestHost to a non-existent class
  62         private void priv_invoke() {
  63             System.out.println("MissingNestHost::priv_invoke");
  64         }
  65     }
  66 
  67     // Helper class adds a level of indirection to avoid the main class
  68     // failing verification if these tests are written directly in main.
  69     // That can only happen if using invokespecial for nestmate invocation.
  70     static class Helper {
  71         static void doTest() {
  72             try {
  73                 MissingNestHost m = new MissingNestHost();
  74                 m.priv_invoke();
  75                 throw new Error("Unexpected success invoking MissingNestHost.priv_invoke");
  76             }
  77             catch (IllegalAccessError iae) {
  78                 if (iae.getMessage().contains("java.lang.NoClassDefFoundError: NoSuchClass")) {
  79                     System.out.println("Got expected exception:" + iae);
  80                 } else {
  81                     throw new Error("Unexpected exception", iae);
  82                 }
  83             }
  84         }
  85     }
  86 
  87     public static void main(String[] args) throws Throwable {
  88         // some errors change depending on whether they are caught by the
  89         // verifier first
  90         boolean verifying = Boolean.parseBoolean(args[0]);
  91         System.out.println("Verification is " +
  92                            (verifying ? "enabled" : "disabled"));
  93 
  94         try {
  95             MissingMethod m = new MissingMethod();
  96             m.priv_invoke();
  97             throw new Error("Unexpected success invoking MissingMethod.priv_invoke");
  98         }
  99         catch (NoSuchMethodError nsme) {
 100             System.out.println("Got expected exception:" + nsme);
 101         }
 102 
 103         // This test was revised to expect successful invocation of the
 104         // super class method - see JDK-8211065
 105         MissingMethodWithSuper m = new MissingMethodWithSuper();
 106         m.priv_invoke();
 107 
 108         // Verification of Helper will trigger the nestmate access check failure
 109         try {
 110             Helper.doTest();
 111         }
 112         catch (IllegalAccessError iae) {
 113             if (verifying)
 114                 System.out.println("Got expected exception:" + iae);
 115             else
 116                 throw new Error("Unexpected error loading Helper class with verification disabled", iae);
 117         }
 118     }
 119 }