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(float actual, float expected, String msg)35 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 check(double actual, double expected, String msg)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 check(Object actual, Object expected, String msg)49 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 getUnsafe()56 private static Unsafe getUnsafe() throws NoSuchFieldException, IllegalAccessException { 57 Class<?> unsafeClass = Unsafe.class; 58 Field f = unsafeClass.getDeclaredField("theUnsafe"); 59 f.setAccessible(true); 60 return (Unsafe) f.get(null); 61 } 62 main(String[] args)63 public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException { 64 System.loadLibrary(args[0]); 65 Unsafe unsafe = getUnsafe(); 66 67 testArrayBaseOffset(unsafe); 68 testArrayIndexScale(unsafe); 69 testGetAndPutAndCAS(unsafe); 70 testGetAndPutVolatile(unsafe); 71 testCopyMemoryPrimitiveArrays(unsafe); 72 } 73 testArrayBaseOffset(Unsafe unsafe)74 private static void testArrayBaseOffset(Unsafe unsafe) { 75 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[])"); 91 } 92 testArrayIndexScale(Unsafe unsafe)93 private static void testArrayIndexScale(Unsafe unsafe) { 94 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[])"); 110 } 111 testGetAndPutAndCAS(Unsafe unsafe)112 private static void testGetAndPutAndCAS(Unsafe unsafe) throws NoSuchFieldException { 113 TestClass t = new TestClass(); 114 115 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)"); 122 123 long longValue = 1234567887654321L; 124 Field longField = TestClass.class.getDeclaredField("longVar"); 125 long longOffset = unsafe.objectFieldOffset(longField); 126 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)"); 130 131 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 139 if (unsafe.compareAndSwapInt(t, intOffset, 0, 1)) { 140 System.out.println("Unexpectedly succeeding compareAndSwapInt(t, intOffset, 0, 1)"); 141 } 142 if (!unsafe.compareAndSwapInt(t, intOffset, intValue, 0)) { 143 System.out.println( 144 "Unexpectedly not succeeding compareAndSwapInt(t, intOffset, intValue, 0)"); 145 } 146 if (!unsafe.compareAndSwapInt(t, intOffset, 0, 1)) { 147 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)"); 153 } 154 155 if (unsafe.compareAndSwapLong(t, longOffset, 0, 1)) { 156 System.out.println("Unexpectedly succeeding compareAndSwapLong(t, longOffset, 0, 1)"); 157 } 158 if (!unsafe.compareAndSwapLong(t, longOffset, longValue, 0)) { 159 System.out.println( 160 "Unexpectedly not succeeding compareAndSwapLong(t, longOffset, longValue, 0)"); 161 } 162 if (!unsafe.compareAndSwapLong(t, longOffset, 0, 1)) { 163 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)"); 169 } 170 171 // 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())) { 177 System.out.println("Unexpectedly succeeding " + 178 "compareAndSwapObject(t, objectOffset, new Object(), new Object())"); 179 } 180 Object objectValue2 = new Object(); 181 if (!unsafe.compareAndSwapObject(t, objectOffset, objectValue, objectValue2)) { 182 System.out.println("Unexpectedly not succeeding " + 183 "compareAndSwapObject(t, objectOffset, objectValue, objectValue2)"); 184 } 185 Object objectValue3 = new Object(); 186 if (!unsafe.compareAndSwapObject(t, objectOffset, objectValue2, objectValue3)) { 187 System.out.println("Unexpectedly not succeeding " + 188 "compareAndSwapObject(t, objectOffset, objectValue2, objectValue3)"); 189 } 190 // 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 } 196 // 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)) { 199 System.out.println( 200 "Unexpectedly not succeeding compareAndSwapObject(t, objectOffset, objectValue3, t)"); 201 } 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)) { 205 System.out.println("Unexpectedly not succeeding compareAndSwapObject(t, objectOffset, t, t)"); 206 } 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())) { 210 System.out.println( 211 "Unexpectedly not succeeding compareAndSwapObject(t, objectOffset, t, new Object())"); 212 } 213 } 214 testGetAndPutVolatile(Unsafe unsafe)215 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 255 // Regression test for "copyMemory" operations hitting a DCHECK() for float/double arrays. testCopyMemoryPrimitiveArrays(Unsafe unsafe)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 287 private static class TestClass { 288 public int intVar = 0; 289 public long longVar = 0; 290 public Object objectVar = null; 291 } 292 293 private static class TestVolatileClass { 294 public volatile int volatileIntVar = 0; 295 public volatile long volatileLongVar = 0; 296 public volatile Object volatileObjectVar = null; 297 } 298 vmArrayBaseOffset(Class<?> clazz)299 private static native int vmArrayBaseOffset(Class<?> clazz); vmArrayIndexScale(Class<?> clazz)300 private static native int vmArrayIndexScale(Class<?> clazz); unsafeTestMalloc(long size)301 private static native long unsafeTestMalloc(long size); unsafeTestFree(long memory)302 private static native void unsafeTestFree(long memory); 303 } 304