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 = Unsafe.getUnsafe();
  53     private static int ADDRESS_SIZE;
  54     private static int HEADER_SIZE;
  55 
  56     static {
  57         // When running with CompressedOops on 64-bit platform, the address size
  58         // reported by Unsafe is still 8, while the real reference fields are 4 bytes long.
  59         // Try to guess the reference field size with this naive trick.
  60         try {
  61             long off1 = U.objectFieldOffset(CompressedOopsClass.class.getField("obj1"));
  62             long off2 = U.objectFieldOffset(CompressedOopsClass.class.getField("obj2"));
  63             ADDRESS_SIZE = (int) Math.abs(off2 - off1);
  64             HEADER_SIZE = (int) Math.min(off1, off2);
  65         } catch (NoSuchFieldException e) {
  66             ADDRESS_SIZE = -1;
  67         }
  68     }
  69 
  70     static class CompressedOopsClass {
  71         public Object obj1;
  72         public Object obj2;
  73     }
  74 
  75     public static boolean arePaddedPairwise(Class klass, String field1, String field2) throws Exception {
  76         Field f1 = klass.getField(field1);
  77         Field f2 = klass.getField(field2);
  78 
  79         int diff = offset(f1) - offset(f2);
  80         if (diff < 0) {
  81             // f1 is first
  82             return (offset(f2) - (offset(f1) + getSize(f1))) > 64;
  83         } else {
  84             // f2 is first
  85             return (offset(f1) - (offset(f2) + getSize(f2))) > 64;
  86         }
  87     }
  88 
  89     public static boolean sameLayout(Class klass1, Class klass2) throws Exception {
  90         for (Field f1 : klass1.getDeclaredFields()) {
  91             Field f2 = klass2.getDeclaredField(f1.getName());
  92             if (offset(f1) != offset(f2)) {
  93                 return false;
  94             }
  95         }
  96 
  97         for (Field f2 : klass1.getDeclaredFields()) {
  98             Field f1 = klass2.getDeclaredField(f2.getName());
  99             if (offset(f1) != offset(f2)) {
 100                 return false;
 101             }
 102         }
 103 
 104         return true;
 105     }
 106 
 107     public static boolean isStatic(Field field) {
 108         return Modifier.isStatic(field.getModifiers());
 109     }
 110 
 111     public static int offset(Field field) {
 112         if (isStatic(field)) {
 113             return (int) U.staticFieldOffset(field);
 114         } else {
 115             return (int) U.objectFieldOffset(field);
 116         }
 117     }
 118 
 119     public static int getSize(Field field) {
 120         Class type = field.getType();
 121         if (type == byte.class)    { return 1; }
 122         if (type == boolean.class) { return 1; }
 123         if (type == short.class)   { return 2; }
 124         if (type == char.class)    { return 2; }
 125         if (type == int.class)     { return 4; }
 126         if (type == float.class)   { return 4; }
 127         if (type == long.class)    { return 8; }
 128         if (type == double.class)  { return 8; }
 129         return ADDRESS_SIZE;
 130     }
 131 
 132     public static void main(String[] args) throws Exception {
 133         boolean endResult = true;
 134 
 135         // --------------- INSTANCE FIELDS ---------------------
 136 
 137         if (!arePaddedPairwise(A2_R1.class, "int1", "int2")) {
 138             System.err.println("A2_R1 failed");
 139             endResult &= false;
 140         }
 141 
 142         if (!arePaddedPairwise(A3_R1.class, "int1", "int2")) {
 143             System.err.println("A3_R1 failed");
 144             endResult &= false;
 145         }
 146 
 147         if (!arePaddedPairwise(A1_R2.class, "int1", "int2")) {
 148             System.err.println("A1_R2 failed");
 149             endResult &= false;
 150         }
 151 
 152         if (!arePaddedPairwise(A2_R2.class, "int1", "int2")) {
 153             System.err.println("A2_R2 failed");
 154             endResult &= false;
 155         }
 156 
 157         if (!arePaddedPairwise(A3_R2.class, "int1", "int2")) {
 158             System.err.println("A3_R2 failed");
 159             endResult &= false;
 160         }
 161 
 162         if (!arePaddedPairwise(A1_R3.class, "int1", "int2")) {
 163             System.err.println("A1_R3 failed");
 164             endResult &= false;
 165         }
 166 
 167         if (!arePaddedPairwise(A2_R3.class, "int1", "int2")) {
 168             System.err.println("A2_R3 failed");
 169             endResult &= false;
 170         }
 171 
 172         if (!arePaddedPairwise(A3_R3.class, "int1", "int2")) {
 173             System.err.println("A3_R3 failed");
 174             endResult &= false;
 175         }
 176 
 177         System.out.println(endResult ? "Test PASSES" : "Test FAILS");
 178         if (!endResult) {
 179            throw new Error("Test failed");
 180         }
 181     }
 182 
 183     public static class R1 {
 184         public int int1;
 185     }
 186 
 187     public static class R2 {
 188         @Contended
 189         public int int1;
 190     }
 191 
 192     @Contended
 193     public static class R3 {
 194         public int int1;
 195     }
 196 
 197     public static class A1_R1 extends R1 {
 198         public int int2;
 199     }
 200 
 201     public static class A2_R1 extends R1 {
 202         @Contended
 203         public int int2;
 204     }
 205 
 206     @Contended
 207     public static class A3_R1 extends R1 {
 208         public int int2;
 209     }
 210 
 211     public static class A1_R2 extends R2 {
 212         public int int2;
 213     }
 214 
 215     public static class A2_R2 extends R2 {
 216         @Contended
 217         public int int2;
 218     }
 219 
 220     @Contended
 221     public static class A3_R2 extends R2 {
 222         public int int2;
 223     }
 224 
 225     public static class A1_R3 extends R3 {
 226         public int int2;
 227     }
 228 
 229     public static class A2_R3 extends R3 {
 230         @Contended
 231         public int int2;
 232     }
 233 
 234     @Contended
 235     public static class A3_R3 extends R3 {
 236         public int int2;
 237     }
 238 
 239 
 240 }
 241