1 /*
   2  * Copyright (c) 2013, 2016, 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 import java.io.BufferedReader;
  25 import java.io.InputStreamReader;
  26 import java.lang.Class;
  27 import java.lang.String;
  28 import java.lang.System;
  29 import java.lang.management.ManagementFactory;
  30 import java.lang.management.RuntimeMXBean;
  31 import java.util.ArrayList;
  32 import java.util.List;
  33 import java.util.concurrent.CyclicBarrier;
  34 import java.util.regex.Matcher;
  35 import java.util.regex.Pattern;
  36 import java.lang.reflect.Field;
  37 import java.lang.reflect.Modifier;
  38 import jdk.internal.misc.Unsafe;
  39 import jdk.internal.vm.annotation.Contended;
  40 
  41 /*
  42  * @test
  43  * @bug     8012939
  44  * @summary \@Contended doesn't work correctly with inheritance
  45  *
  46  * @modules java.base/jdk.internal.misc
  47  * @modules java.base/jdk.internal.vm.annotation
  48  * @run main/othervm -XX:-RestrictContended Inheritance1
  49  */
  50 public class Inheritance1 {
  51 
  52     private static final Unsafe U;
  53     private static int ADDRESS_SIZE;
  54     private static int HEADER_SIZE;
  55 
  56     static {
  57         // steal Unsafe
  58         try {
  59             Field unsafe = Unsafe.class.getDeclaredField("theUnsafe");
  60             unsafe.setAccessible(true);
  61             U = (Unsafe) unsafe.get(null);
  62         } catch (NoSuchFieldException | IllegalAccessException e) {
  63             throw new IllegalStateException(e);
  64         }
  65 
  66         // When running with CompressedOops on 64-bit platform, the address size
  67         // reported by Unsafe is still 8, while the real reference fields are 4 bytes long.
  68         // Try to guess the reference field size with this naive trick.
  69         try {
  70             long off1 = U.objectFieldOffset(CompressedOopsClass.class.getField("obj1"));
  71             long off2 = U.objectFieldOffset(CompressedOopsClass.class.getField("obj2"));
  72             ADDRESS_SIZE = (int) Math.abs(off2 - off1);
  73             HEADER_SIZE = (int) Math.min(off1, off2);
  74         } catch (NoSuchFieldException e) {
  75             ADDRESS_SIZE = -1;
  76         }
  77     }
  78 
  79     static class CompressedOopsClass {
  80         public Object obj1;
  81         public Object obj2;
  82     }
  83 
  84     public static boolean arePaddedPairwise(Class klass, String field1, String field2) throws Exception {
  85         Field f1 = klass.getField(field1);
  86         Field f2 = klass.getField(field2);
  87 
  88         int diff = offset(f1) - offset(f2);
  89         if (diff < 0) {
  90             // f1 is first
  91             return (offset(f2) - (offset(f1) + getSize(f1))) > 64;
  92         } else {
  93             // f2 is first
  94             return (offset(f1) - (offset(f2) + getSize(f2))) > 64;
  95         }
  96     }
  97 
  98     public static boolean sameLayout(Class klass1, Class klass2) throws Exception {
  99         for (Field f1 : klass1.getDeclaredFields()) {
 100             Field f2 = klass2.getDeclaredField(f1.getName());
 101             if (offset(f1) != offset(f2)) {
 102                 return false;
 103             }
 104         }
 105 
 106         for (Field f2 : klass1.getDeclaredFields()) {
 107             Field f1 = klass2.getDeclaredField(f2.getName());
 108             if (offset(f1) != offset(f2)) {
 109                 return false;
 110             }
 111         }
 112 
 113         return true;
 114     }
 115 
 116     public static boolean isStatic(Field field) {
 117         return Modifier.isStatic(field.getModifiers());
 118     }
 119 
 120     public static int offset(Field field) {
 121         if (isStatic(field)) {
 122             return (int) U.staticFieldOffset(field);
 123         } else {
 124             return (int) U.objectFieldOffset(field);
 125         }
 126     }
 127 
 128     public static int getSize(Field field) {
 129         Class type = field.getType();
 130         if (type == byte.class)    { return 1; }
 131         if (type == boolean.class) { return 1; }
 132         if (type == short.class)   { return 2; }
 133         if (type == char.class)    { return 2; }
 134         if (type == int.class)     { return 4; }
 135         if (type == float.class)   { return 4; }
 136         if (type == long.class)    { return 8; }
 137         if (type == double.class)  { return 8; }
 138         return ADDRESS_SIZE;
 139     }
 140 
 141     public static void main(String[] args) throws Exception {
 142         boolean endResult = true;
 143 
 144         // --------------- INSTANCE FIELDS ---------------------
 145 
 146         if (!arePaddedPairwise(A2_R1.class, "int1", "int2")) {
 147             System.err.println("A2_R1 failed");
 148             endResult &= false;
 149         }
 150 
 151         if (!arePaddedPairwise(A3_R1.class, "int1", "int2")) {
 152             System.err.println("A3_R1 failed");
 153             endResult &= false;
 154         }
 155 
 156         if (!arePaddedPairwise(A1_R2.class, "int1", "int2")) {
 157             System.err.println("A1_R2 failed");
 158             endResult &= false;
 159         }
 160 
 161         if (!arePaddedPairwise(A2_R2.class, "int1", "int2")) {
 162             System.err.println("A2_R2 failed");
 163             endResult &= false;
 164         }
 165 
 166         if (!arePaddedPairwise(A3_R2.class, "int1", "int2")) {
 167             System.err.println("A3_R2 failed");
 168             endResult &= false;
 169         }
 170 
 171         if (!arePaddedPairwise(A1_R3.class, "int1", "int2")) {
 172             System.err.println("A1_R3 failed");
 173             endResult &= false;
 174         }
 175 
 176         if (!arePaddedPairwise(A2_R3.class, "int1", "int2")) {
 177             System.err.println("A2_R3 failed");
 178             endResult &= false;
 179         }
 180 
 181         if (!arePaddedPairwise(A3_R3.class, "int1", "int2")) {
 182             System.err.println("A3_R3 failed");
 183             endResult &= false;
 184         }
 185 
 186         System.out.println(endResult ? "Test PASSES" : "Test FAILS");
 187         if (!endResult) {
 188            throw new Error("Test failed");
 189         }
 190     }
 191 
 192     public static class R1 {
 193         public int int1;
 194     }
 195 
 196     public static class R2 {
 197         @Contended
 198         public int int1;
 199     }
 200 
 201     @Contended
 202     public static class R3 {
 203         public int int1;
 204     }
 205 
 206     public static class A1_R1 extends R1 {
 207         public int int2;
 208     }
 209 
 210     public static class A2_R1 extends R1 {
 211         @Contended
 212         public int int2;
 213     }
 214 
 215     @Contended
 216     public static class A3_R1 extends R1 {
 217         public int int2;
 218     }
 219 
 220     public static class A1_R2 extends R2 {
 221         public int int2;
 222     }
 223 
 224     public static class A2_R2 extends R2 {
 225         @Contended
 226         public int int2;
 227     }
 228 
 229     @Contended
 230     public static class A3_R2 extends R2 {
 231         public int int2;
 232     }
 233 
 234     public static class A1_R3 extends R3 {
 235         public int int2;
 236     }
 237 
 238     public static class A2_R3 extends R3 {
 239         @Contended
 240         public int int2;
 241     }
 242 
 243     @Contended
 244     public static class A3_R3 extends R3 {
 245         public int int2;
 246     }
 247 
 248 
 249 }
 250