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     8014509
  44  * @summary \@Contended: explicit default value behaves differently from the implicit value
  45  *
  46  * @modules java.base/jdk.internal.misc
  47  * @modules java.base/jdk.internal.vm.annotation
  48  * @run main/othervm -XX:-RestrictContended DefaultValue
  49  */
  50 public class DefaultValue {
  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         if (!arePaddedPairwise(R1.class, "int1", "int2")) {
 145             System.err.println("R1 failed");
 146             endResult &= false;
 147         }
 148 
 149         if (!arePaddedPairwise(R2.class, "int1", "int2")) {
 150             System.err.println("R2 failed");
 151             endResult &= false;
 152         }
 153 
 154         if (!arePaddedPairwise(R3.class, "int1", "int2")) {
 155             System.err.println("R3 failed");
 156             endResult &= false;
 157         }
 158 
 159         System.out.println(endResult ? "Test PASSES" : "Test FAILS");
 160         if (!endResult) {
 161            throw new Error("Test failed");
 162         }
 163     }
 164 
 165     public static class R1 {
 166         @Contended
 167         public int int1;
 168         @Contended
 169         public int int2;
 170     }
 171 
 172     public static class R2 {
 173         @Contended("")
 174         public int int1;
 175         @Contended("")
 176         public int int2;
 177     }
 178 
 179     public static class R3 {
 180         @Contended()
 181         public int int1;
 182         @Contended()
 183         public int int2;
 184     }
 185 
 186 }
 187