blob: b2f905e0eec5855ce65f58cbf0d22af1f69f3508 [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
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -080042 private static Unsafe getUnsafe() throws Exception {
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
49 public static void main(String[] args) throws Exception {
Mathieu Chartier031768a2015-08-27 10:25:02 -070050 System.loadLibrary(args[0]);
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -080051 Unsafe unsafe = getUnsafe();
52 check(unsafe.arrayBaseOffset(boolean[].class), vmArrayBaseOffset(boolean[].class),
53 "Unsafe.arrayBaseOffset(boolean[])");
54 check(unsafe.arrayBaseOffset(byte[].class), vmArrayBaseOffset(byte[].class),
55 "Unsafe.arrayBaseOffset(byte[])");
56 check(unsafe.arrayBaseOffset(char[].class), vmArrayBaseOffset(char[].class),
57 "Unsafe.arrayBaseOffset(char[])");
58 check(unsafe.arrayBaseOffset(double[].class), vmArrayBaseOffset(double[].class),
59 "Unsafe.arrayBaseOffset(double[])");
60 check(unsafe.arrayBaseOffset(float[].class), vmArrayBaseOffset(float[].class),
61 "Unsafe.arrayBaseOffset(float[])");
62 check(unsafe.arrayBaseOffset(int[].class), vmArrayBaseOffset(int[].class),
63 "Unsafe.arrayBaseOffset(int[])");
64 check(unsafe.arrayBaseOffset(long[].class), vmArrayBaseOffset(long[].class),
65 "Unsafe.arrayBaseOffset(long[])");
66 check(unsafe.arrayBaseOffset(Object[].class), vmArrayBaseOffset(Object[].class),
67 "Unsafe.arrayBaseOffset(Object[])");
68
69 check(unsafe.arrayIndexScale(boolean[].class), vmArrayIndexScale(boolean[].class),
70 "Unsafe.arrayIndexScale(boolean[])");
71 check(unsafe.arrayIndexScale(byte[].class), vmArrayIndexScale(byte[].class),
72 "Unsafe.arrayIndexScale(byte[])");
73 check(unsafe.arrayIndexScale(char[].class), vmArrayIndexScale(char[].class),
74 "Unsafe.arrayIndexScale(char[])");
75 check(unsafe.arrayIndexScale(double[].class), vmArrayIndexScale(double[].class),
76 "Unsafe.arrayIndexScale(double[])");
77 check(unsafe.arrayIndexScale(float[].class), vmArrayIndexScale(float[].class),
78 "Unsafe.arrayIndexScale(float[])");
79 check(unsafe.arrayIndexScale(int[].class), vmArrayIndexScale(int[].class),
80 "Unsafe.arrayIndexScale(int[])");
81 check(unsafe.arrayIndexScale(long[].class), vmArrayIndexScale(long[].class),
82 "Unsafe.arrayIndexScale(long[])");
83 check(unsafe.arrayIndexScale(Object[].class), vmArrayIndexScale(Object[].class),
84 "Unsafe.arrayIndexScale(Object[])");
Vladimir Marko99f391e2014-04-03 12:56:06 +010085
86 TestClass t = new TestClass();
Roland Levillainca1476f2015-06-16 18:09:26 +010087
Vladimir Marko99f391e2014-04-03 12:56:06 +010088 int intValue = 12345678;
89 Field intField = TestClass.class.getDeclaredField("intVar");
90 long intOffset = unsafe.objectFieldOffset(intField);
91 check(unsafe.getInt(t, intOffset), 0, "Unsafe.getInt(Object, long) - initial");
92 unsafe.putInt(t, intOffset, intValue);
93 check(t.intVar, intValue, "Unsafe.putInt(Object, long, int)");
94 check(unsafe.getInt(t, intOffset), intValue, "Unsafe.getInt(Object, long)");
Roland Levillainca1476f2015-06-16 18:09:26 +010095
96 long longValue = 1234567887654321L;
Vladimir Marko99f391e2014-04-03 12:56:06 +010097 Field longField = TestClass.class.getDeclaredField("longVar");
98 long longOffset = unsafe.objectFieldOffset(longField);
Vladimir Marko99f391e2014-04-03 12:56:06 +010099 check(unsafe.getLong(t, longOffset), 0, "Unsafe.getLong(Object, long) - initial");
100 unsafe.putLong(t, longOffset, longValue);
101 check(t.longVar, longValue, "Unsafe.putLong(Object, long, long)");
102 check(unsafe.getLong(t, longOffset), longValue, "Unsafe.getLong(Object, long)");
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800103
Roland Levillainca1476f2015-06-16 18:09:26 +0100104 Object objectValue = new Object();
105 Field objectField = TestClass.class.getDeclaredField("objectVar");
106 long objectOffset = unsafe.objectFieldOffset(objectField);
107 check(unsafe.getObject(t, objectOffset), null, "Unsafe.getObject(Object, long) - initial");
108 unsafe.putObject(t, objectOffset, objectValue);
109 check(t.objectVar, objectValue, "Unsafe.putObject(Object, long, Object)");
110 check(unsafe.getObject(t, objectOffset), objectValue, "Unsafe.getObject(Object, long)");
111
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800112 if (unsafe.compareAndSwapInt(t, intOffset, 0, 1)) {
Roland Levillain2e50ecb2016-01-27 14:08:33 +0000113 System.out.println("Unexpectedly succeeding compareAndSwapInt(t, intOffset, 0, 1)");
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800114 }
115 if (!unsafe.compareAndSwapInt(t, intOffset, intValue, 0)) {
Roland Levillain2e50ecb2016-01-27 14:08:33 +0000116 System.out.println(
117 "Unexpectedly not succeeding compareAndSwapInt(t, intOffset, intValue, 0)");
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800118 }
119 if (!unsafe.compareAndSwapInt(t, intOffset, 0, 1)) {
Roland Levillain2e50ecb2016-01-27 14:08:33 +0000120 System.out.println("Unexpectedly not succeeding compareAndSwapInt(t, intOffset, 0, 1)");
121 }
122 // Exercise sun.misc.Unsafe.compareAndSwapInt using the same
123 // integer (1) for the `expectedValue` and `newValue` arguments.
124 if (!unsafe.compareAndSwapInt(t, intOffset, 1, 1)) {
125 System.out.println("Unexpectedly not succeeding compareAndSwapInt(t, intOffset, 1, 1)");
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800126 }
Mark Mendell58d25fd2015-04-03 14:52:31 -0400127
128 if (unsafe.compareAndSwapLong(t, longOffset, 0, 1)) {
Roland Levillain2e50ecb2016-01-27 14:08:33 +0000129 System.out.println("Unexpectedly succeeding compareAndSwapLong(t, longOffset, 0, 1)");
Mark Mendell58d25fd2015-04-03 14:52:31 -0400130 }
131 if (!unsafe.compareAndSwapLong(t, longOffset, longValue, 0)) {
Roland Levillain2e50ecb2016-01-27 14:08:33 +0000132 System.out.println(
133 "Unexpectedly not succeeding compareAndSwapLong(t, longOffset, longValue, 0)");
Mark Mendell58d25fd2015-04-03 14:52:31 -0400134 }
135 if (!unsafe.compareAndSwapLong(t, longOffset, 0, 1)) {
Roland Levillain2e50ecb2016-01-27 14:08:33 +0000136 System.out.println("Unexpectedly not succeeding compareAndSwapLong(t, longOffset, 0, 1)");
137 }
138 // Exercise sun.misc.Unsafe.compareAndSwapLong using the same
139 // integer (1) for the `expectedValue` and `newValue` arguments.
140 if (!unsafe.compareAndSwapLong(t, longOffset, 1, 1)) {
141 System.out.println("Unexpectedly not succeeding compareAndSwapLong(t, longOffset, 1, 1)");
Mark Mendell58d25fd2015-04-03 14:52:31 -0400142 }
Roland Levillainb550c2e2015-06-26 10:44:53 +0100143
Roland Levillainb488b782015-10-22 11:38:49 +0100144 // We do not use `null` as argument to sun.misc.Unsafe.compareAndSwapObject
145 // in those tests, as this value is not affected by heap poisoning
146 // (which uses address negation to poison and unpoison heap object
147 // references). This way, when heap poisoning is enabled, we can
148 // better exercise its implementation within that method.
149 if (unsafe.compareAndSwapObject(t, objectOffset, new Object(), new Object())) {
Roland Levillain2e50ecb2016-01-27 14:08:33 +0000150 System.out.println("Unexpectedly succeeding " +
151 "compareAndSwapObject(t, objectOffset, new Object(), new Object())");
Roland Levillainb550c2e2015-06-26 10:44:53 +0100152 }
Roland Levillainb488b782015-10-22 11:38:49 +0100153 Object objectValue2 = new Object();
154 if (!unsafe.compareAndSwapObject(t, objectOffset, objectValue, objectValue2)) {
Roland Levillain2e50ecb2016-01-27 14:08:33 +0000155 System.out.println("Unexpectedly not succeeding " +
156 "compareAndSwapObject(t, objectOffset, objectValue, objectValue2)");
Roland Levillainb550c2e2015-06-26 10:44:53 +0100157 }
Roland Levillainb488b782015-10-22 11:38:49 +0100158 Object objectValue3 = new Object();
159 if (!unsafe.compareAndSwapObject(t, objectOffset, objectValue2, objectValue3)) {
Roland Levillain2e50ecb2016-01-27 14:08:33 +0000160 System.out.println("Unexpectedly not succeeding " +
161 "compareAndSwapObject(t, objectOffset, objectValue2, objectValue3)");
Roland Levillainb488b782015-10-22 11:38:49 +0100162 }
Roland Levillain2e50ecb2016-01-27 14:08:33 +0000163 // Exercise sun.misc.Unsafe.compareAndSwapObject using the same
164 // object (`objectValue3`) for the `expectedValue` and `newValue` arguments.
165 if (!unsafe.compareAndSwapObject(t, objectOffset, objectValue3, objectValue3)) {
166 System.out.println("Unexpectedly not succeeding " +
167 "compareAndSwapObject(t, objectOffset, objectValue3, objectValue3)");
168 }
Roland Levillainb488b782015-10-22 11:38:49 +0100169 // Exercise sun.misc.Unsafe.compareAndSwapObject using the same
170 // object (`t`) for the `obj` and `newValue` arguments.
171 if (!unsafe.compareAndSwapObject(t, objectOffset, objectValue3, t)) {
Roland Levillain2e50ecb2016-01-27 14:08:33 +0000172 System.out.println(
173 "Unexpectedly not succeeding compareAndSwapObject(t, objectOffset, objectValue3, t)");
Roland Levillainb488b782015-10-22 11:38:49 +0100174 }
175 // Exercise sun.misc.Unsafe.compareAndSwapObject using the same
176 // object (`t`) for the `obj`, `expectedValue` and `newValue` arguments.
177 if (!unsafe.compareAndSwapObject(t, objectOffset, t, t)) {
Roland Levillain2e50ecb2016-01-27 14:08:33 +0000178 System.out.println("Unexpectedly not succeeding compareAndSwapObject(t, objectOffset, t, t)");
Roland Levillainb488b782015-10-22 11:38:49 +0100179 }
180 // Exercise sun.misc.Unsafe.compareAndSwapObject using the same
181 // object (`t`) for the `obj` and `expectedValue` arguments.
182 if (!unsafe.compareAndSwapObject(t, objectOffset, t, new Object())) {
Roland Levillain2e50ecb2016-01-27 14:08:33 +0000183 System.out.println(
184 "Unexpectedly not succeeding compareAndSwapObject(t, objectOffset, t, new Object())");
Roland Levillainb550c2e2015-06-26 10:44:53 +0100185 }
Vladimir Marko99f391e2014-04-03 12:56:06 +0100186 }
187
188 private static class TestClass {
189 public int intVar = 0;
190 public long longVar = 0;
Roland Levillainca1476f2015-06-16 18:09:26 +0100191 public Object objectVar = null;
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -0800192 }
193
194 private static native int vmArrayBaseOffset(Class clazz);
195 private static native int vmArrayIndexScale(Class clazz);
196}