1 /*
   2  * Copyright (c) 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 package compiler.cha;
  25 
  26 import jdk.internal.vm.annotation.DontInline;
  27 import static compiler.cha.StrengthReduceInterfaceCall.*;
  28 
  29 public class ThreeLevelHierarchyAbstractVsDefault extends ATest<ThreeLevelHierarchyAbstractVsDefault.I> {
  30     public ThreeLevelHierarchyAbstractVsDefault() {
  31         super(I.class, C.class);
  32     }
  33 
  34     interface J1                { default Object m() { return WRONG; } } // intf J1.m DEFAULT
  35     interface J2 extends J1     { Object m(); }                          // intf J2.m ABSTRACT <: intf J1
  36     interface I  extends J1, J2 {}                                       // intf  I.m OVERPASS <: intf J1,J2
  37 
  38     static class C  implements I { public Object m() { return CORRECT; }}
  39 
  40     @DontInline
  41     public Object test(I i) {
  42         return i.m(); // intf I.m OVERPASS
  43     }
  44 
  45     static class DI implements I { public Object m() { return WRONG;   }}
  46 
  47     static class DJ11 implements J1 {}
  48     static class DJ12 implements J1 { public Object m() { return WRONG; }}
  49 
  50     static class DJ2 implements J2 { public Object m() { return WRONG;   }}
  51 
  52     interface K11 extends J1 {}
  53     interface K12 extends J1 { Object m(); }
  54     interface K13 extends J1 { default Object m() { return WRONG; }}
  55     interface K21 extends J2 {}
  56     interface K22 extends J2 { Object m(); }
  57     interface K23 extends J2 { default Object m() { return WRONG; }}
  58 
  59     public void testMega1() {
  60         // 0. Trigger compilation of megamorphic call site
  61         compile(megamorphic()); // C1,C2,C3 <: C.m <: intf I.m OVERPASS <: intf J2.m ABSTRACT <: intf J1.m DEFAULT
  62         assertCompiled();
  63 
  64         // Dependency: type = unique_concrete_method, context = I, method = C.m
  65 
  66         checkInvalidReceiver(); // ensure proper type check on receiver is preserved
  67 
  68         // 1. No deopt/invalidation on not-yet-seen receiver
  69         repeat(100, () -> call(new C(){})); // Cn <: C.m <: intf I.m OVERPASS <: intf J2.m ABSTRACT <: intf J1.m DEFAULT
  70         assertCompiled();
  71 
  72         // 2. No dependency invalidation: different context
  73         initialize(K11.class, K12.class, K13.class,
  74                 K21.class, K22.class, K23.class);
  75 
  76         // 3. Dependency invalidation: Cn.m <: C <: I
  77         call(new C() { public Object m() { return CORRECT; }}); // Cn.m <: C.m <: intf I.m OVERPASS <: intf J2.m ABSTRACT <: intf J1.m DEFAULT
  78         assertNotCompiled();
  79 
  80         // 4. Recompilation w/o a dependency
  81         compile(megamorphic());
  82         call(new C() { public Object m() { return CORRECT; }});
  83         assertCompiled(); // no inlining
  84 
  85         checkInvalidReceiver(); // ensure proper type check on receiver is preserved
  86     }
  87 
  88     public void testMega2() {
  89         // 0. Trigger compilation of a megamorphic call site
  90         compile(megamorphic());
  91         assertCompiled();
  92 
  93         // Dependency: type = unique_concrete_method, context = I, method = C.m
  94 
  95         checkInvalidReceiver(); // ensure proper type check on receiver is preserved
  96 
  97         // 1. No dependency invalidation: different context
  98         initialize(DJ11.class,
  99                 DJ12.class,
 100                 DJ2.class);
 101         assertCompiled();
 102 
 103         // 2. Dependency invalidation: DI.m <: I
 104         initialize(DI.class);
 105         assertNotCompiled();
 106 
 107         // 3. Recompilation w/o a dependency
 108         compile(megamorphic());
 109         call(new C() { public Object m() { return CORRECT; }});
 110         assertCompiled(); // no inlining
 111 
 112         checkInvalidReceiver(); // ensure proper type check on receiver is preserved
 113     }
 114 
 115     @Override
 116     public void checkInvalidReceiver() {
 117         shouldThrow(IncompatibleClassChangeError.class, () -> {
 118             I o = (I) unsafeCastMH(I.class).invokeExact(new Object()); // unrelated
 119             test(o);
 120         });
 121         assertCompiled();
 122 
 123         shouldThrow(IncompatibleClassChangeError.class, () -> {
 124             I j = (I) unsafeCastMH(I.class).invokeExact((Object) new J1() {
 125             }); // super interface
 126             test(j);
 127         });
 128         assertCompiled();
 129 
 130         shouldThrow(IncompatibleClassChangeError.class, () -> {
 131             I j = (I) unsafeCastMH(I.class).invokeExact((Object) new J2() {
 132                 public Object m() {
 133                     return WRONG;
 134                 }
 135             }); // super interface
 136             test(j);
 137         });
 138         assertCompiled();
 139     }
 140 }
 141