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