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