1 /*
2  * Copyright (c) 2016, 2020, 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 /*
25  * @test
26  * @bug 8161720
27  * @modules java.base/jdk.internal.misc
28  * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -Xbatch -XX:-TieredCompilation UnsafeSmallOffsetBooleanAccessTest
29  * @run main/othervm -Xbatch UnsafeSmallOffsetBooleanAccessTest
30  */
31 
32 import java.util.Random;
33 import jdk.internal.misc.Unsafe;
34 
35 public class UnsafeSmallOffsetBooleanAccessTest {
36     static final Unsafe UNSAFE = Unsafe.getUnsafe();
37     static final long F_OFFSET;
38     static final Random random = new Random(42);
39 
40     static {
41         try {
42             F_OFFSET = UNSAFE.objectFieldOffset(T.class.getDeclaredField("f"));
43             System.out.println("The offset is: " + F_OFFSET);
44         } catch (Exception e) {
45             throw new Error(e);
46         }
47     }
48 
49     static class T {
50         boolean f;
51     }
52 
53     // Always return false in a way that is not obvious to the compiler.
myRandom()54     public static boolean myRandom() {
55         if (random.nextInt(101) > 134) {
56             return true;
57         } else {
58             return false;
59         }
60     }
61 
test(T t)62     public static boolean test(T t) {
63         boolean result = false;
64         for (int i = 0; i < 20000; i++) {
65             boolean random = myRandom();
66             // If myRandom() returns false, access t.f.
67             //
68             // If myRandom() returns true, access virtual address
69             // F_OFFSET. That address is most likely not mapped,
70             // therefore the access will most likely cause a
71             // crash. We're not concerned about that, though, because
72             // myRandom() always returns false. However, the C2
73             // compiler avoids normalization of the value returned by
74             // getBoolean in this case.
75             result = UNSAFE.getBoolean(myRandom() ? null : t, F_OFFSET);
76         }
77         return result;
78     }
79 
main(String[] args)80     public static void main(String[] args) {
81         T t = new T();
82         UNSAFE.putBoolean(t, F_OFFSET, true);
83         System.out.println("The result for t is: " + test(t));
84     }
85 }
86