• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 import java.lang.reflect.Field;
18 import sun.misc.Unsafe;
19 
20 public class Main {
check(int actual, int expected, String msg)21   private static void check(int actual, int expected, String msg) {
22     if (actual != expected) {
23       System.out.println(msg + " : " + actual + " != " + expected);
24       System.exit(1);
25     }
26   }
27 
check(long actual, long expected, String msg)28   private static void check(long actual, long expected, String msg) {
29     if (actual != expected) {
30       System.out.println(msg + " : " + actual + " != " + expected);
31       System.exit(1);
32     }
33   }
34 
check(Object actual, Object expected, String msg)35   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 
getUnsafe()42   private static Unsafe getUnsafe() throws NoSuchFieldException, IllegalAccessException {
43     Class<?> unsafeClass = Unsafe.class;
44     Field f = unsafeClass.getDeclaredField("theUnsafe");
45     f.setAccessible(true);
46     return (Unsafe) f.get(null);
47   }
48 
main(String[] args)49   public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
50     System.loadLibrary(args[0]);
51     Unsafe unsafe = getUnsafe();
52 
53     testArrayBaseOffset(unsafe);
54     testArrayIndexScale(unsafe);
55     testGetAndPutAndCAS(unsafe);
56     testGetAndPutVolatile(unsafe);
57   }
58 
testArrayBaseOffset(Unsafe unsafe)59   private static void testArrayBaseOffset(Unsafe unsafe) {
60     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[])");
76   }
77 
testArrayIndexScale(Unsafe unsafe)78   private static void testArrayIndexScale(Unsafe unsafe) {
79     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[])");
95   }
96 
testGetAndPutAndCAS(Unsafe unsafe)97   private static void testGetAndPutAndCAS(Unsafe unsafe) throws NoSuchFieldException {
98     TestClass t = new TestClass();
99 
100     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)");
107 
108     long longValue = 1234567887654321L;
109     Field longField = TestClass.class.getDeclaredField("longVar");
110     long longOffset = unsafe.objectFieldOffset(longField);
111     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)");
115 
116     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 
124     if (unsafe.compareAndSwapInt(t, intOffset, 0, 1)) {
125       System.out.println("Unexpectedly succeeding compareAndSwapInt(t, intOffset, 0, 1)");
126     }
127     if (!unsafe.compareAndSwapInt(t, intOffset, intValue, 0)) {
128       System.out.println(
129           "Unexpectedly not succeeding compareAndSwapInt(t, intOffset, intValue, 0)");
130     }
131     if (!unsafe.compareAndSwapInt(t, intOffset, 0, 1)) {
132       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)");
138     }
139 
140     if (unsafe.compareAndSwapLong(t, longOffset, 0, 1)) {
141       System.out.println("Unexpectedly succeeding compareAndSwapLong(t, longOffset, 0, 1)");
142     }
143     if (!unsafe.compareAndSwapLong(t, longOffset, longValue, 0)) {
144       System.out.println(
145           "Unexpectedly not succeeding compareAndSwapLong(t, longOffset, longValue, 0)");
146     }
147     if (!unsafe.compareAndSwapLong(t, longOffset, 0, 1)) {
148       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)");
154     }
155 
156     // 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())) {
162       System.out.println("Unexpectedly succeeding " +
163           "compareAndSwapObject(t, objectOffset, new Object(), new Object())");
164     }
165     Object objectValue2 = new Object();
166     if (!unsafe.compareAndSwapObject(t, objectOffset, objectValue, objectValue2)) {
167       System.out.println("Unexpectedly not succeeding " +
168           "compareAndSwapObject(t, objectOffset, objectValue, objectValue2)");
169     }
170     Object objectValue3 = new Object();
171     if (!unsafe.compareAndSwapObject(t, objectOffset, objectValue2, objectValue3)) {
172       System.out.println("Unexpectedly not succeeding " +
173           "compareAndSwapObject(t, objectOffset, objectValue2, objectValue3)");
174     }
175     // 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     }
181     // 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)) {
184       System.out.println(
185           "Unexpectedly not succeeding compareAndSwapObject(t, objectOffset, objectValue3, t)");
186     }
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)) {
190       System.out.println("Unexpectedly not succeeding compareAndSwapObject(t, objectOffset, t, t)");
191     }
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())) {
195       System.out.println(
196           "Unexpectedly not succeeding compareAndSwapObject(t, objectOffset, t, new Object())");
197     }
198   }
199 
testGetAndPutVolatile(Unsafe unsafe)200   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 
240   private static class TestClass {
241     public int intVar = 0;
242     public long longVar = 0;
243     public Object objectVar = null;
244   }
245 
246   private static class TestVolatileClass {
247     public volatile int volatileIntVar = 0;
248     public volatile long volatileLongVar = 0;
249     public volatile Object volatileObjectVar = null;
250   }
251 
vmArrayBaseOffset(Class<?> clazz)252   private static native int vmArrayBaseOffset(Class<?> clazz);
vmArrayIndexScale(Class<?> clazz)253   private static native int vmArrayIndexScale(Class<?> clazz);
254 }
255