blob: 9176e89aafc67472901e7762a3b2b4a23c4d5ba9 [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
Vladimir Marko104883b2018-11-09 17:12:23 +000035 private static void check(float actual, float expected, String msg) {
36 if (actual != expected) {
37 System.out.println(msg + " : " + actual + " != " + expected);
38 System.exit(1);
39 }
40 }
41
42 private static void check(double actual, double expected, String msg) {
43 if (actual != expected) {
44 System.out.println(msg + " : " + actual + " != " + expected);
45 System.exit(1);
46 }
47 }
48
Roland Levillainca1476f2015-06-16 18:09:26 +010049 private static void check(Object actual, Object expected, String msg) {
50 if (actual != expected) {
51 System.out.println(msg + " : " + actual + " != " + expected);
52 System.exit(1);
53 }
54 }
55
Roland Levillain3d312422016-06-23 13:53:42 +010056 private static Unsafe getUnsafe() throws NoSuchFieldException, IllegalAccessException {
Aart Bik0e54c012016-03-04 12:08:31 -080057 Class<?> unsafeClass = Unsafe.class;
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -080058 Field f = unsafeClass.getDeclaredField("theUnsafe");
59 f.setAccessible(true);
60 return (Unsafe) f.get(null);
61 }
62
Roland Levillain3d312422016-06-23 13:53:42 +010063 public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
Mathieu Chartier031768a2015-08-27 10:25:02 -070064 System.loadLibrary(args[0]);
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -080065 Unsafe unsafe = getUnsafe();
Roland Levillain3d312422016-06-23 13:53:42 +010066
67 testArrayBaseOffset(unsafe);
68 testArrayIndexScale(unsafe);
69 testGetAndPutAndCAS(unsafe);
70 testGetAndPutVolatile(unsafe);
Vladimir Marko104883b2018-11-09 17:12:23 +000071 testCopyMemoryPrimitiveArrays(unsafe);
Roland Levillain3d312422016-06-23 13:53:42 +010072 }
73
74 private static void testArrayBaseOffset(Unsafe unsafe) {
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -080075 check(unsafe.arrayBaseOffset(boolean[].class), vmArrayBaseOffset(boolean[].class),
76 "Unsafe.arrayBaseOffset(boolean[])");
77 check(unsafe.arrayBaseOffset(byte[].class), vmArrayBaseOffset(byte[].class),
78 "Unsafe.arrayBaseOffset(byte[])");
79 check(unsafe.arrayBaseOffset(char[].class), vmArrayBaseOffset(char[].class),
80 "Unsafe.arrayBaseOffset(char[])");
81 check(unsafe.arrayBaseOffset(double[].class), vmArrayBaseOffset(double[].class),
82 "Unsafe.arrayBaseOffset(double[])");
83 check(unsafe.arrayBaseOffset(float[].class), vmArrayBaseOffset(float[].class),
84 "Unsafe.arrayBaseOffset(float[])");
85 check(unsafe.arrayBaseOffset(int[].class), vmArrayBaseOffset(int[].class),
86 "Unsafe.arrayBaseOffset(int[])");
87 check(unsafe.arrayBaseOffset(long[].class), vmArrayBaseOffset(long[].class),
88 "Unsafe.arrayBaseOffset(long[])");
89 check(unsafe.arrayBaseOffset(Object[].class), vmArrayBaseOffset(Object[].class),
90 "Unsafe.arrayBaseOffset(Object[])");
Roland Levillain3d312422016-06-23 13:53:42 +010091 }
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -080092
Roland Levillain3d312422016-06-23 13:53:42 +010093 private static void testArrayIndexScale(Unsafe unsafe) {
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -080094 check(unsafe.arrayIndexScale(boolean[].class), vmArrayIndexScale(boolean[].class),
95 "Unsafe.arrayIndexScale(boolean[])");
96 check(unsafe.arrayIndexScale(byte[].class), vmArrayIndexScale(byte[].class),
97 "Unsafe.arrayIndexScale(byte[])");
98 check(unsafe.arrayIndexScale(char[].class), vmArrayIndexScale(char[].class),
99 "Unsafe.arrayIndexScale(char[])");
100 check(unsafe.arrayIndexScale(double[].class), vmArrayIndexScale(double[].class),
101 "Unsafe.arrayIndexScale(double[])");
102 check(unsafe.arrayIndexScale(float[].class), vmArrayIndexScale(float[].class),
103 "Unsafe.arrayIndexScale(float[])");
104 check(unsafe.arrayIndexScale(int[].class), vmArrayIndexScale(int[].class),
105 "Unsafe.arrayIndexScale(int[])");
106 check(unsafe.arrayIndexScale(long[].class), vmArrayIndexScale(long[].class),
107 "Unsafe.arrayIndexScale(long[])");
108 check(unsafe.arrayIndexScale(Object[].class), vmArrayIndexScale(Object[].class),
109 "Unsafe.arrayIndexScale(Object[])");
Roland Levillain3d312422016-06-23 13:53:42 +0100110 }
Vladimir Marko99f391e2014-04-03 12:56:06 +0100111
Roland Levillain3d312422016-06-23 13:53:42 +0100112 private static void testGetAndPutAndCAS(Unsafe unsafe) throws NoSuchFieldException {
Vladimir Marko99f391e2014-04-03 12:56:06 +0100113 TestClass t = new TestClass();
Roland Levillainca1476f2015-06-16 18:09:26 +0100114
Vladimir Marko99f391e2014-04-03 12:56:06 +0100115 int intValue = 12345678;
116 Field intField = TestClass.class.getDeclaredField("intVar");
117 long intOffset = unsafe.objectFieldOffset(intField);
118 check(unsafe.getInt(t, intOffset), 0, "Unsafe.getInt(Object, long) - initial");
119 unsafe.putInt(t, intOffset, intValue);
120 check(t.intVar, intValue, "Unsafe.putInt(Object, long, int)");
121 check(unsafe.getInt(t, intOffset), intValue, "Unsafe.getInt(Object, long)");
Roland Levillainca1476f2015-06-16 18:09:26 +0100122
123 long longValue = 1234567887654321L;
Vladimir Marko99f391e2014-04-03 12:56:06 +0100124 Field longField = TestClass.class.getDeclaredField("longVar");
125 long longOffset = unsafe.objectFieldOffset(longField);
Vladimir Marko99f391e2014-04-03 12:56:06 +0100126 check(unsafe.getLong(t, longOffset), 0, "Unsafe.getLong(Object, long) - initial");
127 unsafe.putLong(t, longOffset, longValue);
128 check(t.longVar, longValue, "Unsafe.putLong(Object, long, long)");
129 check(unsafe.getLong(t, longOffset), longValue, "Unsafe.getLong(Object, long)");
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800130
Roland Levillainca1476f2015-06-16 18:09:26 +0100131 Object objectValue = new Object();
132 Field objectField = TestClass.class.getDeclaredField("objectVar");
133 long objectOffset = unsafe.objectFieldOffset(objectField);
134 check(unsafe.getObject(t, objectOffset), null, "Unsafe.getObject(Object, long) - initial");
135 unsafe.putObject(t, objectOffset, objectValue);
136 check(t.objectVar, objectValue, "Unsafe.putObject(Object, long, Object)");
137 check(unsafe.getObject(t, objectOffset), objectValue, "Unsafe.getObject(Object, long)");
138
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800139 if (unsafe.compareAndSwapInt(t, intOffset, 0, 1)) {
Roland Levillain2e50ecb2016-01-27 14:08:33 +0000140 System.out.println("Unexpectedly succeeding compareAndSwapInt(t, intOffset, 0, 1)");
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800141 }
142 if (!unsafe.compareAndSwapInt(t, intOffset, intValue, 0)) {
Roland Levillain2e50ecb2016-01-27 14:08:33 +0000143 System.out.println(
144 "Unexpectedly not succeeding compareAndSwapInt(t, intOffset, intValue, 0)");
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800145 }
146 if (!unsafe.compareAndSwapInt(t, intOffset, 0, 1)) {
Roland Levillain2e50ecb2016-01-27 14:08:33 +0000147 System.out.println("Unexpectedly not succeeding compareAndSwapInt(t, intOffset, 0, 1)");
148 }
149 // Exercise sun.misc.Unsafe.compareAndSwapInt using the same
150 // integer (1) for the `expectedValue` and `newValue` arguments.
151 if (!unsafe.compareAndSwapInt(t, intOffset, 1, 1)) {
152 System.out.println("Unexpectedly not succeeding compareAndSwapInt(t, intOffset, 1, 1)");
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800153 }
Mark Mendell58d25fd2015-04-03 14:52:31 -0400154
155 if (unsafe.compareAndSwapLong(t, longOffset, 0, 1)) {
Roland Levillain2e50ecb2016-01-27 14:08:33 +0000156 System.out.println("Unexpectedly succeeding compareAndSwapLong(t, longOffset, 0, 1)");
Mark Mendell58d25fd2015-04-03 14:52:31 -0400157 }
158 if (!unsafe.compareAndSwapLong(t, longOffset, longValue, 0)) {
Roland Levillain2e50ecb2016-01-27 14:08:33 +0000159 System.out.println(
160 "Unexpectedly not succeeding compareAndSwapLong(t, longOffset, longValue, 0)");
Mark Mendell58d25fd2015-04-03 14:52:31 -0400161 }
162 if (!unsafe.compareAndSwapLong(t, longOffset, 0, 1)) {
Roland Levillain2e50ecb2016-01-27 14:08:33 +0000163 System.out.println("Unexpectedly not succeeding compareAndSwapLong(t, longOffset, 0, 1)");
164 }
165 // Exercise sun.misc.Unsafe.compareAndSwapLong using the same
166 // integer (1) for the `expectedValue` and `newValue` arguments.
167 if (!unsafe.compareAndSwapLong(t, longOffset, 1, 1)) {
168 System.out.println("Unexpectedly not succeeding compareAndSwapLong(t, longOffset, 1, 1)");
Mark Mendell58d25fd2015-04-03 14:52:31 -0400169 }
Roland Levillainb550c2e2015-06-26 10:44:53 +0100170
Roland Levillainb488b782015-10-22 11:38:49 +0100171 // We do not use `null` as argument to sun.misc.Unsafe.compareAndSwapObject
172 // in those tests, as this value is not affected by heap poisoning
173 // (which uses address negation to poison and unpoison heap object
174 // references). This way, when heap poisoning is enabled, we can
175 // better exercise its implementation within that method.
176 if (unsafe.compareAndSwapObject(t, objectOffset, new Object(), new Object())) {
Roland Levillain2e50ecb2016-01-27 14:08:33 +0000177 System.out.println("Unexpectedly succeeding " +
178 "compareAndSwapObject(t, objectOffset, new Object(), new Object())");
Roland Levillainb550c2e2015-06-26 10:44:53 +0100179 }
Roland Levillainb488b782015-10-22 11:38:49 +0100180 Object objectValue2 = new Object();
181 if (!unsafe.compareAndSwapObject(t, objectOffset, objectValue, objectValue2)) {
Roland Levillain2e50ecb2016-01-27 14:08:33 +0000182 System.out.println("Unexpectedly not succeeding " +
183 "compareAndSwapObject(t, objectOffset, objectValue, objectValue2)");
Roland Levillainb550c2e2015-06-26 10:44:53 +0100184 }
Roland Levillainb488b782015-10-22 11:38:49 +0100185 Object objectValue3 = new Object();
186 if (!unsafe.compareAndSwapObject(t, objectOffset, objectValue2, objectValue3)) {
Roland Levillain2e50ecb2016-01-27 14:08:33 +0000187 System.out.println("Unexpectedly not succeeding " +
188 "compareAndSwapObject(t, objectOffset, objectValue2, objectValue3)");
Roland Levillainb488b782015-10-22 11:38:49 +0100189 }
Roland Levillain2e50ecb2016-01-27 14:08:33 +0000190 // Exercise sun.misc.Unsafe.compareAndSwapObject using the same
191 // object (`objectValue3`) for the `expectedValue` and `newValue` arguments.
192 if (!unsafe.compareAndSwapObject(t, objectOffset, objectValue3, objectValue3)) {
193 System.out.println("Unexpectedly not succeeding " +
194 "compareAndSwapObject(t, objectOffset, objectValue3, objectValue3)");
195 }
Roland Levillainb488b782015-10-22 11:38:49 +0100196 // Exercise sun.misc.Unsafe.compareAndSwapObject using the same
197 // object (`t`) for the `obj` and `newValue` arguments.
198 if (!unsafe.compareAndSwapObject(t, objectOffset, objectValue3, t)) {
Roland Levillain2e50ecb2016-01-27 14:08:33 +0000199 System.out.println(
200 "Unexpectedly not succeeding compareAndSwapObject(t, objectOffset, objectValue3, t)");
Roland Levillainb488b782015-10-22 11:38:49 +0100201 }
202 // Exercise sun.misc.Unsafe.compareAndSwapObject using the same
203 // object (`t`) for the `obj`, `expectedValue` and `newValue` arguments.
204 if (!unsafe.compareAndSwapObject(t, objectOffset, t, t)) {
Roland Levillain2e50ecb2016-01-27 14:08:33 +0000205 System.out.println("Unexpectedly not succeeding compareAndSwapObject(t, objectOffset, t, t)");
Roland Levillainb488b782015-10-22 11:38:49 +0100206 }
207 // Exercise sun.misc.Unsafe.compareAndSwapObject using the same
208 // object (`t`) for the `obj` and `expectedValue` arguments.
209 if (!unsafe.compareAndSwapObject(t, objectOffset, t, new Object())) {
Roland Levillain2e50ecb2016-01-27 14:08:33 +0000210 System.out.println(
211 "Unexpectedly not succeeding compareAndSwapObject(t, objectOffset, t, new Object())");
Roland Levillainb550c2e2015-06-26 10:44:53 +0100212 }
Vladimir Marko99f391e2014-04-03 12:56:06 +0100213 }
214
Roland Levillain3d312422016-06-23 13:53:42 +0100215 private static void testGetAndPutVolatile(Unsafe unsafe) throws NoSuchFieldException {
216 TestVolatileClass tv = new TestVolatileClass();
217
218 int intValue = 12345678;
219 Field volatileIntField = TestVolatileClass.class.getDeclaredField("volatileIntVar");
220 long volatileIntOffset = unsafe.objectFieldOffset(volatileIntField);
221 check(unsafe.getIntVolatile(tv, volatileIntOffset),
222 0,
223 "Unsafe.getIntVolatile(Object, long) - initial");
224 unsafe.putIntVolatile(tv, volatileIntOffset, intValue);
225 check(tv.volatileIntVar, intValue, "Unsafe.putIntVolatile(Object, long, int)");
226 check(unsafe.getIntVolatile(tv, volatileIntOffset),
227 intValue,
228 "Unsafe.getIntVolatile(Object, long)");
229
230 long longValue = 1234567887654321L;
231 Field volatileLongField = TestVolatileClass.class.getDeclaredField("volatileLongVar");
232 long volatileLongOffset = unsafe.objectFieldOffset(volatileLongField);
233 check(unsafe.getLongVolatile(tv, volatileLongOffset),
234 0,
235 "Unsafe.getLongVolatile(Object, long) - initial");
236 unsafe.putLongVolatile(tv, volatileLongOffset, longValue);
237 check(tv.volatileLongVar, longValue, "Unsafe.putLongVolatile(Object, long, long)");
238 check(unsafe.getLongVolatile(tv, volatileLongOffset),
239 longValue,
240 "Unsafe.getLongVolatile(Object, long)");
241
242 Object objectValue = new Object();
243 Field volatileObjectField = TestVolatileClass.class.getDeclaredField("volatileObjectVar");
244 long volatileObjectOffset = unsafe.objectFieldOffset(volatileObjectField);
245 check(unsafe.getObjectVolatile(tv, volatileObjectOffset),
246 null,
247 "Unsafe.getObjectVolatile(Object, long) - initial");
248 unsafe.putObjectVolatile(tv, volatileObjectOffset, objectValue);
249 check(tv.volatileObjectVar, objectValue, "Unsafe.putObjectVolatile(Object, long, Object)");
250 check(unsafe.getObjectVolatile(tv, volatileObjectOffset),
251 objectValue,
252 "Unsafe.getObjectVolatile(Object, long)");
253 }
254
Vladimir Marko104883b2018-11-09 17:12:23 +0000255 // Regression test for "copyMemory" operations hitting a DCHECK() for float/double arrays.
256 private static void testCopyMemoryPrimitiveArrays(Unsafe unsafe) {
257 int size = 4 * 1024;
258 long memory = unsafeTestMalloc(size);
259
260 int floatSize = 4;
261 float[] inputFloats = new float[size / floatSize];
262 for (int i = 0; i != inputFloats.length; ++i) {
263 inputFloats[i] = ((float)i) + 0.5f;
264 }
265 float[] outputFloats = new float[size / floatSize];
266 unsafe.copyMemoryFromPrimitiveArray(inputFloats, 0, memory, size);
267 unsafe.copyMemoryToPrimitiveArray(memory, outputFloats, 0, size);
268 for (int i = 0; i != inputFloats.length; ++i) {
269 check(inputFloats[i], outputFloats[i], "unsafe.copyMemory/float");
270 }
271
272 int doubleSize = 8;
273 double[] inputDoubles = new double[size / doubleSize];
274 for (int i = 0; i != inputDoubles.length; ++i) {
275 inputDoubles[i] = ((double)i) + 0.5;
276 }
277 double[] outputDoubles = new double[size / doubleSize];
278 unsafe.copyMemoryFromPrimitiveArray(inputDoubles, 0, memory, size);
279 unsafe.copyMemoryToPrimitiveArray(memory, outputDoubles, 0, size);
280 for (int i = 0; i != inputDoubles.length; ++i) {
281 check(inputDoubles[i], outputDoubles[i], "unsafe.copyMemory/double");
282 }
283
284 unsafeTestFree(memory);
285 }
286
Vladimir Marko99f391e2014-04-03 12:56:06 +0100287 private static class TestClass {
288 public int intVar = 0;
289 public long longVar = 0;
Roland Levillainca1476f2015-06-16 18:09:26 +0100290 public Object objectVar = null;
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -0800291 }
292
Roland Levillain3d312422016-06-23 13:53:42 +0100293 private static class TestVolatileClass {
294 public volatile int volatileIntVar = 0;
295 public volatile long volatileLongVar = 0;
296 public volatile Object volatileObjectVar = null;
297 }
298
Andreas Gampe166aaee2016-07-18 08:27:23 -0700299 private static native int vmArrayBaseOffset(Class<?> clazz);
300 private static native int vmArrayIndexScale(Class<?> clazz);
Vladimir Marko104883b2018-11-09 17:12:23 +0000301 private static native long unsafeTestMalloc(long size);
302 private static native void unsafeTestFree(long memory);
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -0800303}