1 /*
   2  * Copyright (c) 2017, 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 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 (NoClassDefFoundError ncdfe) {
  78                 System.out.println("Got expected exception:" + ncdfe);
  79             }
  80         }
  81     }
  82 
  83     public static void main(String[] args) throws Throwable {
  84         // some errors change depending on whether they are caught by the
  85         // verifier first
  86         boolean verifying = Boolean.parseBoolean(args[0]);
  87         System.out.println("Verification is " +
  88                            (verifying ? "enabled" : "disabled"));
  89 
  90         try {
  91             MissingMethod m = new MissingMethod();
  92             m.priv_invoke();
  93             throw new Error("Unexpected success invoking MissingMethod.priv_invoke");
  94         }
  95         catch (NoSuchMethodError nsme) {
  96             System.out.println("Got expected exception:" + nsme);
  97         }
  98 
  99         // This test was revised to expect successful invocation of the
 100         // super class method - see JDK-8211065
 101         MissingMethodWithSuper m = new MissingMethodWithSuper();
 102         m.priv_invoke();
 103 
 104         // Verification of Helper will trigger the nestmate access check failure
 105         try {
 106             Helper.doTest();
 107         }
 108         catch (NoClassDefFoundError ncdfe) {
 109             if (verifying)
 110                 System.out.println("Got expected exception:" + ncdfe);
 111             else
 112                 throw new Error("Unexpected error loading Helper class with verification disabled");
 113         }
 114     }
 115 }