blob: d43d374a4248a36022a6f322c99213dd4686db9c [file] [log] [blame]
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -08001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17import java.lang.reflect.Field;
18import sun.misc.Unsafe;
19
Andreas Gampe1c83cbc2014-07-22 18:52:29 -070020public class Main {
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -080021 private static void check(int actual, int expected, String msg) {
22 if (actual != expected) {
Andreas Gampe1c83cbc2014-07-22 18:52:29 -070023 System.out.println(msg + " : " + actual + " != " + expected);
Brian Carlstromda4442e2014-10-14 15:39:01 -070024 System.exit(1);
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -080025 }
26 }
27
Vladimir Marko99f391e2014-04-03 12:56:06 +010028 private static void check(long actual, long expected, String msg) {
29 if (actual != expected) {
Andreas Gampe1c83cbc2014-07-22 18:52:29 -070030 System.out.println(msg + " : " + actual + " != " + expected);
Brian Carlstromda4442e2014-10-14 15:39:01 -070031 System.exit(1);
Vladimir Marko99f391e2014-04-03 12:56:06 +010032 }
33 }
34
Roland Levillainca1476f2015-06-16 18:09:26 +010035 private static void check(Object actual, Object expected, String msg) {
36 if (actual != expected) {
37 System.out.println(msg + " : " + actual + " != " + expected);
38 System.exit(1);
39 }
40 }
41
Roland Levillain3d312422016-06-23 13:53:42 +010042 private static Unsafe getUnsafe() throws NoSuchFieldException, IllegalAccessException {
Aart Bik0e54c012016-03-04 12:08:31 -080043 Class<?> unsafeClass = Unsafe.class;
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -080044 Field f = unsafeClass.getDeclaredField("theUnsafe");
45 f.setAccessible(true);
46 return (Unsafe) f.get(null);
47 }
48
Roland Levillain3d312422016-06-23 13:53:42 +010049 public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
Mathieu Chartier031768a2015-08-27 10:25:02 -070050 System.loadLibrary(args[0]);
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -080051 Unsafe unsafe = getUnsafe();
Roland Levillain3d312422016-06-23 13:53:42 +010052
53 testArrayBaseOffset(unsafe);
54 testArrayIndexScale(unsafe);
55 testGetAndPutAndCAS(unsafe);
56 testGetAndPutVolatile(unsafe);
57 }
58
59 private static void testArrayBaseOffset(Unsafe unsafe) {
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -080060 check(unsafe.arrayBaseOffset(boolean[].class), vmArrayBaseOffset(boolean[].class),
61 "Unsafe.arrayBaseOffset(boolean[])");
62 check(unsafe.arrayBaseOffset(byte[].class), vmArrayBaseOffset(byte[].class),
63 "Unsafe.arrayBaseOffset(byte[])");
64 check(unsafe.arrayBaseOffset(char[].class), vmArrayBaseOffset(char[].class),
65 "Unsafe.arrayBaseOffset(char[])");
66 check(unsafe.arrayBaseOffset(double[].class), vmArrayBaseOffset(double[].class),
67 "Unsafe.arrayBaseOffset(double[])");
68 check(unsafe.arrayBaseOffset(float[].class), vmArrayBaseOffset(float[].class),
69 "Unsafe.arrayBaseOffset(float[])");
70 check(unsafe.arrayBaseOffset(int[].class), vmArrayBaseOffset(int[].class),
71 "Unsafe.arrayBaseOffset(int[])");
72 check(unsafe.arrayBaseOffset(long[].class), vmArrayBaseOffset(long[].class),
73 "Unsafe.arrayBaseOffset(long[])");
74 check(unsafe.arrayBaseOffset(Object[].class), vmArrayBaseOffset(Object[].class),
75 "Unsafe.arrayBaseOffset(Object[])");
Roland Levillain3d312422016-06-23 13:53:42 +010076 }
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -080077
Roland Levillain3d312422016-06-23 13:53:42 +010078 private static void testArrayIndexScale(Unsafe unsafe) {
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -080079 check(unsafe.arrayIndexScale(boolean[].class), vmArrayIndexScale(boolean[].class),
80 "Unsafe.arrayIndexScale(boolean[])");
81 check(unsafe.arrayIndexScale(byte[].class), vmArrayIndexScale(byte[].class),
82 "Unsafe.arrayIndexScale(byte[])");
83 check(unsafe.arrayIndexScale(char[].class), vmArrayIndexScale(char[].class),
84 "Unsafe.arrayIndexScale(char[])");
85 check(unsafe.arrayIndexScale(double[].class), vmArrayIndexScale(double[].class),
86 "Unsafe.arrayIndexScale(double[])");
87 check(unsafe.arrayIndexScale(float[].class), vmArrayIndexScale(float[].class),
88 "Unsafe.arrayIndexScale(float[])");
89 check(unsafe.arrayIndexScale(int[].class), vmArrayIndexScale(int[].class),
90 "Unsafe.arrayIndexScale(int[])");
91 check(unsafe.arrayIndexScale(long[].class), vmArrayIndexScale(long[].class),
92 "Unsafe.arrayIndexScale(long[])");
93 check(unsafe.arrayIndexScale(Object[].class), vmArrayIndexScale(Object[].class),
94 "Unsafe.arrayIndexScale(Object[])");
Roland Levillain3d312422016-06-23 13:53:42 +010095 }
Vladimir Marko99f391e2014-04-03 12:56:06 +010096
Roland Levillain3d312422016-06-23 13:53:42 +010097 private static void testGetAndPutAndCAS(Unsafe unsafe) throws NoSuchFieldException {
Vladimir Marko99f391e2014-04-03 12:56:06 +010098 TestClass t = new TestClass();
Roland Levillainca1476f2015-06-16 18:09:26 +010099
Vladimir Marko99f391e2014-04-03 12:56:06 +0100100 int intValue = 12345678;
101 Field intField = TestClass.class.getDeclaredField("intVar");
102 long intOffset = unsafe.objectFieldOffset(intField);
103 check(unsafe.getInt(t, intOffset), 0, "Unsafe.getInt(Object, long) - initial");
104 unsafe.putInt(t, intOffset, intValue);
105 check(t.intVar, intValue, "Unsafe.putInt(Object, long, int)");
106 check(unsafe.getInt(t, intOffset), intValue, "Unsafe.getInt(Object, long)");
Roland Levillainca1476f2015-06-16 18:09:26 +0100107
108 long longValue = 1234567887654321L;
Vladimir Marko99f391e2014-04-03 12:56:06 +0100109 Field longField = TestClass.class.getDeclaredField("longVar");
110 long longOffset = unsafe.objectFieldOffset(longField);
Vladimir Marko99f391e2014-04-03 12:56:06 +0100111 check(unsafe.getLong(t, longOffset), 0, "Unsafe.getLong(Object, long) - initial");
112 unsafe.putLong(t, longOffset, longValue);
113 check(t.longVar, longValue, "Unsafe.putLong(Object, long, long)");
114 check(unsafe.getLong(t, longOffset), longValue, "Unsafe.getLong(Object, long)");
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800115
Roland Levillainca1476f2015-06-16 18:09:26 +0100116 Object objectValue = new Object();
117 Field objectField = TestClass.class.getDeclaredField("objectVar");
118 long objectOffset = unsafe.objectFieldOffset(objectField);
119 check(unsafe.getObject(t, objectOffset), null, "Unsafe.getObject(Object, long) - initial");
120 unsafe.putObject(t, objectOffset, objectValue);
121 check(t.objectVar, objectValue, "Unsafe.putObject(Object, long, Object)");
122 check(unsafe.getObject(t, objectOffset), objectValue, "Unsafe.getObject(Object, long)");
123
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800124 if (unsafe.compareAndSwapInt(t, intOffset, 0, 1)) {
Roland Levillain2e50ecb2016-01-27 14:08:33 +0000125 System.out.println("Unexpectedly succeeding compareAndSwapInt(t, intOffset, 0, 1)");
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800126 }
127 if (!unsafe.compareAndSwapInt(t, intOffset, intValue, 0)) {
Roland Levillain2e50ecb2016-01-27 14:08:33 +0000128 System.out.println(
129 "Unexpectedly not succeeding compareAndSwapInt(t, intOffset, intValue, 0)");
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800130 }
131 if (!unsafe.compareAndSwapInt(t, intOffset, 0, 1)) {
Roland Levillain2e50ecb2016-01-27 14:08:33 +0000132 System.out.println("Unexpectedly not succeeding compareAndSwapInt(t, intOffset, 0, 1)");
133 }
134 // Exercise sun.misc.Unsafe.compareAndSwapInt using the same
135 // integer (1) for the `expectedValue` and `newValue` arguments.
136 if (!unsafe.compareAndSwapInt(t, intOffset, 1, 1)) {
137 System.out.println("Unexpectedly not succeeding compareAndSwapInt(t, intOffset, 1, 1)");
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800138 }
Mark Mendell58d25fd2015-04-03 14:52:31 -0400139
140 if (unsafe.compareAndSwapLong(t, longOffset, 0, 1)) {
Roland Levillain2e50ecb2016-01-27 14:08:33 +0000141 System.out.println("Unexpectedly succeeding compareAndSwapLong(t, longOffset, 0, 1)");
Mark Mendell58d25fd2015-04-03 14:52:31 -0400142 }
143 if (!unsafe.compareAndSwapLong(t, longOffset, longValue, 0)) {
Roland Levillain2e50ecb2016-01-27 14:08:33 +0000144 System.out.println(
145 "Unexpectedly not succeeding compareAndSwapLong(t, longOffset, longValue, 0)");
Mark Mendell58d25fd2015-04-03 14:52:31 -0400146 }
147 if (!unsafe.compareAndSwapLong(t, longOffset, 0, 1)) {
Roland Levillain2e50ecb2016-01-27 14:08:33 +0000148 System.out.println("Unexpectedly not succeeding compareAndSwapLong(t, longOffset, 0, 1)");
149 }
150 // Exercise sun.misc.Unsafe.compareAndSwapLong using the same
151 // integer (1) for the `expectedValue` and `newValue` arguments.
152 if (!unsafe.compareAndSwapLong(t, longOffset, 1, 1)) {
153 System.out.println("Unexpectedly not succeeding compareAndSwapLong(t, longOffset, 1, 1)");
Mark Mendell58d25fd2015-04-03 14:52:31 -0400154 }
Roland Levillainb550c2e2015-06-26 10:44:53 +0100155
Roland Levillainb488b782015-10-22 11:38:49 +0100156 // We do not use `null` as argument to sun.misc.Unsafe.compareAndSwapObject
157 // in those tests, as this value is not affected by heap poisoning
158 // (which uses address negation to poison and unpoison heap object
159 // references). This way, when heap poisoning is enabled, we can
160 // better exercise its implementation within that method.
161 if (unsafe.compareAndSwapObject(t, objectOffset, new Object(), new Object())) {
Roland Levillain2e50ecb2016-01-27 14:08:33 +0000162 System.out.println("Unexpectedly succeeding " +
163 "compareAndSwapObject(t, objectOffset, new Object(), new Object())");
Roland Levillainb550c2e2015-06-26 10:44:53 +0100164 }
Roland Levillainb488b782015-10-22 11:38:49 +0100165 Object objectValue2 = new Object();
166 if (!unsafe.compareAndSwapObject(t, objectOffset, objectValue, objectValue2)) {
Roland Levillain2e50ecb2016-01-27 14:08:33 +0000167 System.out.println("Unexpectedly not succeeding " +
168 "compareAndSwapObject(t, objectOffset, objectValue, objectValue2)");
Roland Levillainb550c2e2015-06-26 10:44:53 +0100169 }
Roland Levillainb488b782015-10-22 11:38:49 +0100170 Object objectValue3 = new Object();
171 if (!unsafe.compareAndSwapObject(t, objectOffset, objectValue2, objectValue3)) {
Roland Levillain2e50ecb2016-01-27 14:08:33 +0000172 System.out.println("Unexpectedly not succeeding " +
173 "compareAndSwapObject(t, objectOffset, objectValue2, objectValue3)");
Roland Levillainb488b782015-10-22 11:38:49 +0100174 }
Roland Levillain2e50ecb2016-01-27 14:08:33 +0000175 // Exercise sun.misc.Unsafe.compareAndSwapObject using the same
176 // object (`objectValue3`) for the `expectedValue` and `newValue` arguments.
177 if (!unsafe.compareAndSwapObject(t, objectOffset, objectValue3, objectValue3)) {
178 System.out.println("Unexpectedly not succeeding " +
179 "compareAndSwapObject(t, objectOffset, objectValue3, objectValue3)");
180 }
Roland Levillainb488b782015-10-22 11:38:49 +0100181 // Exercise sun.misc.Unsafe.compareAndSwapObject using the same
182 // object (`t`) for the `obj` and `newValue` arguments.
183 if (!unsafe.compareAndSwapObject(t, objectOffset, objectValue3, t)) {
Roland Levillain2e50ecb2016-01-27 14:08:33 +0000184 System.out.println(
185 "Unexpectedly not succeeding compareAndSwapObject(t, objectOffset, objectValue3, t)");
Roland Levillainb488b782015-10-22 11:38:49 +0100186 }
187 // Exercise sun.misc.Unsafe.compareAndSwapObject using the same
188 // object (`t`) for the `obj`, `expectedValue` and `newValue` arguments.
189 if (!unsafe.compareAndSwapObject(t, objectOffset, t, t)) {
Roland Levillain2e50ecb2016-01-27 14:08:33 +0000190 System.out.println("Unexpectedly not succeeding compareAndSwapObject(t, objectOffset, t, t)");
Roland Levillainb488b782015-10-22 11:38:49 +0100191 }
192 // Exercise sun.misc.Unsafe.compareAndSwapObject using the same
193 // object (`t`) for the `obj` and `expectedValue` arguments.
194 if (!unsafe.compareAndSwapObject(t, objectOffset, t, new Object())) {
Roland Levillain2e50ecb2016-01-27 14:08:33 +0000195 System.out.println(
196 "Unexpectedly not succeeding compareAndSwapObject(t, objectOffset, t, new Object())");
Roland Levillainb550c2e2015-06-26 10:44:53 +0100197 }
Vladimir Marko99f391e2014-04-03 12:56:06 +0100198 }
199
Roland Levillain3d312422016-06-23 13:53:42 +0100200 private static void testGetAndPutVolatile(Unsafe unsafe) throws NoSuchFieldException {
201 TestVolatileClass tv = new TestVolatileClass();
202
203 int intValue = 12345678;
204 Field volatileIntField = TestVolatileClass.class.getDeclaredField("volatileIntVar");
205 long volatileIntOffset = unsafe.objectFieldOffset(volatileIntField);
206 check(unsafe.getIntVolatile(tv, volatileIntOffset),
207 0,
208 "Unsafe.getIntVolatile(Object, long) - initial");
209 unsafe.putIntVolatile(tv, volatileIntOffset, intValue);
210 check(tv.volatileIntVar, intValue, "Unsafe.putIntVolatile(Object, long, int)");
211 check(unsafe.getIntVolatile(tv, volatileIntOffset),
212 intValue,
213 "Unsafe.getIntVolatile(Object, long)");
214
215 long longValue = 1234567887654321L;
216 Field volatileLongField = TestVolatileClass.class.getDeclaredField("volatileLongVar");
217 long volatileLongOffset = unsafe.objectFieldOffset(volatileLongField);
218 check(unsafe.getLongVolatile(tv, volatileLongOffset),
219 0,
220 "Unsafe.getLongVolatile(Object, long) - initial");
221 unsafe.putLongVolatile(tv, volatileLongOffset, longValue);
222 check(tv.volatileLongVar, longValue, "Unsafe.putLongVolatile(Object, long, long)");
223 check(unsafe.getLongVolatile(tv, volatileLongOffset),
224 longValue,
225 "Unsafe.getLongVolatile(Object, long)");
226
227 Object objectValue = new Object();
228 Field volatileObjectField = TestVolatileClass.class.getDeclaredField("volatileObjectVar");
229 long volatileObjectOffset = unsafe.objectFieldOffset(volatileObjectField);
230 check(unsafe.getObjectVolatile(tv, volatileObjectOffset),
231 null,
232 "Unsafe.getObjectVolatile(Object, long) - initial");
233 unsafe.putObjectVolatile(tv, volatileObjectOffset, objectValue);
234 check(tv.volatileObjectVar, objectValue, "Unsafe.putObjectVolatile(Object, long, Object)");
235 check(unsafe.getObjectVolatile(tv, volatileObjectOffset),
236 objectValue,
237 "Unsafe.getObjectVolatile(Object, long)");
238 }
239
Vladimir Marko99f391e2014-04-03 12:56:06 +0100240 private static class TestClass {
241 public int intVar = 0;
242 public long longVar = 0;
Roland Levillainca1476f2015-06-16 18:09:26 +0100243 public Object objectVar = null;
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -0800244 }
245
Roland Levillain3d312422016-06-23 13:53:42 +0100246 private static class TestVolatileClass {
247 public volatile int volatileIntVar = 0;
248 public volatile long volatileLongVar = 0;
249 public volatile Object volatileObjectVar = null;
250 }
251
Andreas Gampe166aaee2016-07-18 08:27:23 -0700252 private static native int vmArrayBaseOffset(Class<?> clazz);
253 private static native int vmArrayIndexScale(Class<?> clazz);
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -0800254}