test/hotspot/jtreg/runtime/exceptionMsgs/IncompatibleClassChangeError/IncompatibleClassChangeErrorTest.java
changeset 50633 b8055b38b252
parent 49469 1708db7f94c6
child 50761 cb07f4b539fc
equal deleted inserted replaced
50632:fd430e352427 50633:b8055b38b252
    29  * @requires !(os.arch=="arm") & vm.flavor == "server" & !vm.emulatedClient & vm.compMode=="Xmixed" & (!vm.graal.enabled | vm.opt.TieredCompilation == true) & (vm.opt.TieredStopAtLevel == null | vm.opt.TieredStopAtLevel==4)
    29  * @requires !(os.arch=="arm") & vm.flavor == "server" & !vm.emulatedClient & vm.compMode=="Xmixed" & (!vm.graal.enabled | vm.opt.TieredCompilation == true) & (vm.opt.TieredStopAtLevel == null | vm.opt.TieredStopAtLevel==4)
    30  * @library /test/lib /
    30  * @library /test/lib /
    31  * @build sun.hotspot.WhiteBox
    31  * @build sun.hotspot.WhiteBox
    32  * @run driver ClassFileInstaller sun.hotspot.WhiteBox sun.hotspot.WhiteBox$WhiteBoxPermission
    32  * @run driver ClassFileInstaller sun.hotspot.WhiteBox sun.hotspot.WhiteBox$WhiteBoxPermission
    33  * @compile IncompatibleClassChangeErrorTest.java
    33  * @compile IncompatibleClassChangeErrorTest.java
    34  * @compile ImplementsSomeInterfaces.jasm ICC_B.jasm
    34  * @compile ImplementsSomeInterfaces.jasm ICC2_B.jasm ICC3_B.jasm ICC4_B.jasm
    35  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
    35  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
    36  *                   -XX:CompileThreshold=1000 -XX:-BackgroundCompilation -XX:-Inline
    36  *                   -XX:CompileThreshold=1000 -XX:-BackgroundCompilation -XX:-Inline
    37  *                   -XX:CompileCommand=exclude,IncompatibleClassChangeErrorTest::test_iccInt
    37  *                   -XX:CompileCommand=exclude,test.IncompatibleClassChangeErrorTest::test_iccInt
    38  *                   IncompatibleClassChangeErrorTest
    38  *                   test.IncompatibleClassChangeErrorTest
    39  */
    39  */
       
    40 
       
    41 package test;
    40 
    42 
    41 import sun.hotspot.WhiteBox;
    43 import sun.hotspot.WhiteBox;
    42 import compiler.whitebox.CompilerWhiteBoxTest;
    44 import compiler.whitebox.CompilerWhiteBoxTest;
    43 import java.lang.reflect.Method;
    45 import java.lang.reflect.Method;
    44 
    46 
    45 // This test assembles an errorneous installation of classes.
    47 // This test assembles an erroneous installation of classes.
    46 // First, compile the test by @compile. This results in a legal set
    48 // First, compile the test by @compile. This results in a legal set
    47 // of classes.
    49 // of classes.
    48 // Then, with jasm, generate incompatible classes that overwrite
    50 // Then, with jasm, generate incompatible classes that overwrite
    49 // the class files in the build directory.
    51 // the class files in the build directory.
    50 // Last, call the real tests throwing IncompatibleClassChangeErrors
    52 // Last, call the real tests throwing IncompatibleClassChangeErrors
    54     private static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
    56     private static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
    55 
    57 
    56     private static boolean enableChecks = true;
    58     private static boolean enableChecks = true;
    57 
    59 
    58     private static String expectedErrorMessageInterpreted =
    60     private static String expectedErrorMessageInterpreted =
    59         "Class ImplementsSomeInterfaces " +
    61         "Class test.ImplementsSomeInterfaces " +
    60         "does not implement the requested interface InterfaceICCE1";
    62         "does not implement the requested interface test.InterfaceICCE1";
    61     private static String expectedErrorMessageCompiled =
    63     private static String expectedErrorMessageCompiled =
    62         "Class ICC_B does not implement the requested interface ICC_iB";
    64         "Class test.ICC2_B does not implement the requested interface test.ICC2_iB";
    63         // old message: "vtable stub"
    65         // old message: "vtable stub"
    64 
    66 
    65 
    67 
    66     private static boolean compile(Class<?> clazz, String name) {
    68     private static boolean compile(Class<?> clazz, String name) {
    67         try {
    69         try {
    91         test_icc_compiled_itable_stub();
    93         test_icc_compiled_itable_stub();
    92         enableChecks = true;
    94         enableChecks = true;
    93 
    95 
    94         // Compile
    96         // Compile
    95         if (!compile(IncompatibleClassChangeErrorTest.class, "test_icc_compiled_itable_stub") ||
    97         if (!compile(IncompatibleClassChangeErrorTest.class, "test_icc_compiled_itable_stub") ||
    96             !compile(ICC_C.class, "b") ||
    98             !compile(ICC2_C.class, "b") ||
    97             !compile(ICC_D.class, "b") ||
    99             !compile(ICC2_D.class, "b") ||
    98             !compile(ICC_E.class, "b")) {
   100             !compile(ICC2_E.class, "b")) {
    99           return false;
   101           return false;
   100         }
   102         }
   101 
   103 
   102         System.out.println("warmup done.");
   104         System.out.println("warmup done.");
   103         return true;
   105         return true;
   117             if (enableChecks && !errorMsg.equals(expectedErrorMessageInterpreted)) {
   119             if (enableChecks && !errorMsg.equals(expectedErrorMessageInterpreted)) {
   118                 System.out.println("Expected: " + expectedErrorMessageInterpreted + "\n" +
   120                 System.out.println("Expected: " + expectedErrorMessageInterpreted + "\n" +
   119                                    "but got:  " + errorMsg);
   121                                    "but got:  " + errorMsg);
   120                 throw new RuntimeException("Wrong error message of IncompatibleClassChangeError.");
   122                 throw new RuntimeException("Wrong error message of IncompatibleClassChangeError.");
   121             }
   123             }
       
   124             if (enableChecks) {
       
   125                 System.out.println("Test 1 passed with message: " + errorMsg);
       
   126             }
   122             caught_icc = true;
   127             caught_icc = true;
   123         } catch (Throwable e) {
   128         } catch (Throwable e) {
   124             throw new RuntimeException("Caught unexpected exception: " + e);
   129             throw new RuntimeException("Caught unexpected exception: " + e);
   125         }
   130         }
   126 
   131 
   140     //   re-resolve, which happens constantly during the first 10 iterations of the loop.
   145     //   re-resolve, which happens constantly during the first 10 iterations of the loop.
   141     // => we should be fine! :-)
   146     // => we should be fine! :-)
   142     public static void test_icc_compiled_itable_stub() {
   147     public static void test_icc_compiled_itable_stub() {
   143         // Allocated the objects we need and call a valid method.
   148         // Allocated the objects we need and call a valid method.
   144         boolean caught_icc = false;
   149         boolean caught_icc = false;
   145         ICC_B b = new ICC_B();
   150         ICC2_B b = new ICC2_B();
   146         ICC_C c = new ICC_C();
   151         ICC2_C c = new ICC2_C();
   147         ICC_D d = new ICC_D();
   152         ICC2_D d = new ICC2_D();
   148         ICC_E e = new ICC_E();
   153         ICC2_E e = new ICC2_E();
   149         b.a();
   154         b.a();
   150         c.a();
   155         c.a();
   151         d.a();
   156         d.a();
   152         e.a();
   157         e.a();
   153 
   158 
   154         try {
   159         try {
   155             final int iterations = 10;
   160             final int iterations = 10;
   156             // Test: calls b.b() in the last iteration.
   161             // Test: calls b.b() in the last iteration.
   157             for (int i = 0; i < iterations; i++) {
   162             for (int i = 0; i < iterations; i++) {
   158                 ICC_iB a = b;
   163                 ICC2_iB a = b;
   159                 if (i % 3 == 0 && i < iterations - 1) {
   164                 if (i % 3 == 0 && i < iterations - 1) {
   160                     a = c;
   165                     a = c;
   161                 }
   166                 }
   162                 if (i % 3 == 1 && i < iterations - 1) {
   167                 if (i % 3 == 1 && i < iterations - 1) {
   163                     a = d;
   168                     a = d;
   192                                    "but got:  " + errorMsg);
   197                                    "but got:  " + errorMsg);
   193                 System.out.println(exc);
   198                 System.out.println(exc);
   194                 throw new RuntimeException("Wrong error message of IncompatibleClassChangeError.");
   199                 throw new RuntimeException("Wrong error message of IncompatibleClassChangeError.");
   195             }
   200             }
   196             if (enableChecks) {
   201             if (enableChecks) {
   197                 System.out.println("Passed with message: " + errorMsg);
   202                 System.out.println("Test 2 passed with message: " + errorMsg);
   198             }
   203             }
   199         } catch (Throwable exc) {
   204         } catch (Throwable exc) {
   200             throw exc; // new RuntimeException("Caught unexpected exception: " + exc);
   205             throw exc; // new RuntimeException("Caught unexpected exception: " + exc);
   201         }
   206         }
   202 
   207 
   204         if (enableChecks && !caught_icc) {
   209         if (enableChecks && !caught_icc) {
   205             throw new RuntimeException("Expected IncompatibleClassChangeError was not thrown.");
   210             throw new RuntimeException("Expected IncompatibleClassChangeError was not thrown.");
   206         }
   211         }
   207     }
   212     }
   208 
   213 
       
   214     private static String expectedErrorMessage3 =
       
   215         "Class test.ICC3_B can not implement test.ICC3_A, because it is not an interface";
       
   216 
       
   217     public static void test3_implementsClass() throws Exception {
       
   218         try {
       
   219             new ICC3_B();
       
   220             throw new RuntimeException("Expected IncompatibleClassChangeError was not thrown.");
       
   221         } catch (IncompatibleClassChangeError e) {
       
   222             String errorMsg = e.getMessage();
       
   223             if (!errorMsg.equals(expectedErrorMessage3)) {
       
   224                 System.out.println("Expected: " + expectedErrorMessage3 + "\n" +
       
   225                                    "but got:  " + errorMsg);
       
   226                 throw new RuntimeException("Wrong error message of IncompatibleClassChangeError.");
       
   227             }
       
   228             System.out.println("Test 3 passed with message: " + errorMsg);
       
   229         } catch (Throwable e) {
       
   230             throw new RuntimeException("Caught unexpected exception: " + e);
       
   231         }
       
   232     }
       
   233 
       
   234     private static String expectedErrorMessage4 =
       
   235         "class test.ICC4_B has interface test.ICC4_iA as super class";
       
   236 
       
   237     public static void test4_extendsInterface() throws Exception {
       
   238         try {
       
   239             new ICC4_B();
       
   240             throw new RuntimeException("Expected IncompatibleClassChangeError was not thrown.");
       
   241         } catch (IncompatibleClassChangeError e) {
       
   242             String errorMsg = e.getMessage();
       
   243             if (!errorMsg.equals(expectedErrorMessage4)) {
       
   244                 System.out.println("Expected: " + expectedErrorMessage4 + "\n" +
       
   245                                    "but got:  " + errorMsg);
       
   246                 throw new RuntimeException("Wrong error message of IncompatibleClassChangeError.");
       
   247             }
       
   248             System.out.println("Test 4 passed with message: " + errorMsg);
       
   249         } catch (Throwable e) {
       
   250             throw new RuntimeException("Caught unexpected exception: " + e);
       
   251         }
       
   252     }
       
   253 
   209     public static void main(String[] args) throws Exception {
   254     public static void main(String[] args) throws Exception {
   210         if (!setup_test()) {
   255         if (!setup_test()) {
   211             return;
   256             return;
   212         }
   257         }
   213         test_iccInt();
   258         test_iccInt();
   214         test_icc_compiled_itable_stub();
   259         test_icc_compiled_itable_stub();
       
   260         test3_implementsClass();
       
   261         test4_extendsInterface();
   215     }
   262     }
   216 }
   263 }
   217 
   264 
   218 
   265 
   219 // Helper classes to test incompatible class change in interpreter.
   266 // Helper classes to test incompatible class change in interpreter.
   303 //          iA,iB   (interfaces)
   350 //          iA,iB   (interfaces)
   304 //          /|\ \
   351 //          /|\ \
   305 //         C D E \
   352 //         C D E \
   306 //                B (bad class, missing interface implementation)
   353 //                B (bad class, missing interface implementation)
   307 
   354 
   308 interface ICC_iA {
   355 interface ICC2_iA {
   309     public void a();
   356     public void a();
   310 }
   357 }
   311 
   358 
   312 interface ICC_iB {
   359 interface ICC2_iB {
   313     public void b();
   360     public void b();
   314 }
   361 }
   315 
   362 
   316 // This is the errorneous class. A variant of it not
   363 // This is the erroneous class. A variant of it not
   317 // implementing ICC_iB is copied into the test before
   364 // implementing ICC2_iB is copied into the test before
   318 // it is run.
   365 // it is run.
   319 class ICC_B implements ICC_iA,
   366 class ICC2_B implements ICC2_iA,
   320                        // This interface is missing in the .jasm implementation.
   367                        // This interface is missing in the .jasm implementation.
   321                        ICC_iB {
   368                        ICC2_iB {
   322     public void a() {
   369     public void a() {
   323         System.out.print("B.a() ");
   370         System.out.print("B.a() ");
   324     }
   371     }
   325 
   372 
   326     public void b() {
   373     public void b() {
   327         System.out.print("B.b() ");
   374         System.out.print("B.b() ");
   328     }
   375     }
   329 }
   376 }
   330 
   377 
   331 class ICC_C implements ICC_iA, ICC_iB {
   378 class ICC2_C implements ICC2_iA, ICC2_iB {
   332     public void a() {
   379     public void a() {
   333         System.out.print("C.a() ");
   380         System.out.print("C.a() ");
   334     }
   381     }
   335 
   382 
   336     public void b() {
   383     public void b() {
   337         System.out.print("C.b() ");
   384         System.out.print("C.b() ");
   338     }
   385     }
   339 }
   386 }
   340 
   387 
   341 class ICC_D implements ICC_iA, ICC_iB {
   388 class ICC2_D implements ICC2_iA, ICC2_iB {
   342     public void a() {
   389     public void a() {
   343         System.out.print("D.a() ");
   390         System.out.print("D.a() ");
   344     }
   391     }
   345 
   392 
   346     public void b() {
   393     public void b() {
   347         System.out.print("D.b() ");
   394         System.out.print("D.b() ");
   348     }
   395     }
   349 }
   396 }
   350 
   397 
   351 class ICC_E implements ICC_iA, ICC_iB {
   398 class ICC2_E implements ICC2_iA, ICC2_iB {
   352     public void a() {
   399     public void a() {
   353         System.out.print("E.a() ");
   400         System.out.print("E.a() ");
   354     }
   401     }
   355 
   402 
   356     public void b() {
   403     public void b() {
   357         System.out.print("E.b() ");
   404         System.out.print("E.b() ");
   358     }
   405     }
   359 }
   406 }
       
   407 
       
   408 // Helper classes to test error where class appears in implements statement.
       
   409 //
       
   410 // Class hierachy:
       
   411 //
       
   412 //       A  Some Class.
       
   413 //       |
       
   414 //       B  erroneous class. Correct B extends A, incorrect B (from jasm) implements A.
       
   415 
       
   416 class ICC3_A {
       
   417 }
       
   418 
       
   419 class ICC3_B extends ICC3_A {
       
   420 }
       
   421 
       
   422 // Helper classes to test error where interface appears in extends statement.
       
   423 //
       
   424 // Class hierachy:
       
   425 //
       
   426 //       A  Some Interface.
       
   427 //       |
       
   428 //       B  erroneous class. Correct B implements A, incorrect B (from jasm) extends A.
       
   429 
       
   430 interface ICC4_iA {
       
   431 }
       
   432 
       
   433 class ICC4_B implements ICC4_iA {
       
   434 }