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
24import java.io.BufferedReader;
25import java.io.InputStreamReader;
26import java.lang.Class;
27import java.lang.String;
28import java.lang.System;
29import java.lang.management.ManagementFactory;
30import java.lang.management.RuntimeMXBean;
31import java.util.ArrayList;
32import java.util.List;
33import java.util.concurrent.CyclicBarrier;
34import java.util.regex.Matcher;
35import java.util.regex.Pattern;
36import java.lang.reflect.Field;
37import java.lang.reflect.Modifier;
38import jdk.internal.misc.Unsafe;
39import 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 */
50public class DefaultValue {
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        if (!arePaddedPairwise(R1.class, "int1", "int2")) {
136            System.err.println("R1 failed");
137            endResult &= false;
138        }
139
140        if (!arePaddedPairwise(R2.class, "int1", "int2")) {
141            System.err.println("R2 failed");
142            endResult &= false;
143        }
144
145        if (!arePaddedPairwise(R3.class, "int1", "int2")) {
146            System.err.println("R3 failed");
147            endResult &= false;
148        }
149
150        System.out.println(endResult ? "Test PASSES" : "Test FAILS");
151        if (!endResult) {
152           throw new Error("Test failed");
153        }
154    }
155
156    public static class R1 {
157        @Contended
158        public int int1;
159        @Contended
160        public int int2;
161    }
162
163    public static class R2 {
164        @Contended("")
165        public int int1;
166        @Contended("")
167        public int int2;
168    }
169
170    public static class R3 {
171        @Contended()
172        public int int1;
173        @Contended()
174        public int int2;
175    }
176
177}
178
179