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 byte byteValue = 123; 140 Field byteField = TestClass.class.getDeclaredField("byteVar"); 141 long byteOffset = unsafe.objectFieldOffset(byteField); 142 check(unsafe.getByte(t, byteOffset), 0, "Unsafe.getByte(Object, long) - initial"); 143 unsafe.putByte(t, byteOffset, byteValue); 144 check(t.byteVar, byteValue, "Unsafe.putByte(Object, long, byte)"); 145 check(unsafe.getByte(t, byteOffset), byteValue, "Unsafe.getByte(Object, long)"); 146 147 if (unsafe.compareAndSwapInt(t, intOffset, 0, 1)) { 148 System.out.println("Unexpectedly succeeding compareAndSwapInt(t, intOffset, 0, 1)"); 149 } 150 if (!unsafe.compareAndSwapInt(t, intOffset, intValue, 0)) { 151 System.out.println( 152 "Unexpectedly not succeeding compareAndSwapInt(t, intOffset, intValue, 0)"); 153 } 154 if (!unsafe.compareAndSwapInt(t, intOffset, 0, 1)) { 155 System.out.println("Unexpectedly not succeeding compareAndSwapInt(t, intOffset, 0, 1)"); 156 } 157 // Exercise sun.misc.Unsafe.compareAndSwapInt using the same 158 // integer (1) for the `expectedValue` and `newValue` arguments. 159 if (!unsafe.compareAndSwapInt(t, intOffset, 1, 1)) { 160 System.out.println("Unexpectedly not succeeding compareAndSwapInt(t, intOffset, 1, 1)"); 161 } 162 163 if (unsafe.compareAndSwapLong(t, longOffset, 0, 1)) { 164 System.out.println("Unexpectedly succeeding compareAndSwapLong(t, longOffset, 0, 1)"); 165 } 166 if (!unsafe.compareAndSwapLong(t, longOffset, longValue, 0)) { 167 System.out.println( 168 "Unexpectedly not succeeding compareAndSwapLong(t, longOffset, longValue, 0)"); 169 } 170 if (!unsafe.compareAndSwapLong(t, longOffset, 0, 1)) { 171 System.out.println("Unexpectedly not succeeding compareAndSwapLong(t, longOffset, 0, 1)"); 172 } 173 // Exercise sun.misc.Unsafe.compareAndSwapLong using the same 174 // integer (1) for the `expectedValue` and `newValue` arguments. 175 if (!unsafe.compareAndSwapLong(t, longOffset, 1, 1)) { 176 System.out.println("Unexpectedly not succeeding compareAndSwapLong(t, longOffset, 1, 1)"); 177 } 178 179 // We do not use `null` as argument to sun.misc.Unsafe.compareAndSwapObject 180 // in those tests, as this value is not affected by heap poisoning 181 // (which uses address negation to poison and unpoison heap object 182 // references). This way, when heap poisoning is enabled, we can 183 // better exercise its implementation within that method. 184 if (unsafe.compareAndSwapObject(t, objectOffset, new Object(), new Object())) { 185 System.out.println("Unexpectedly succeeding " + 186 "compareAndSwapObject(t, objectOffset, new Object(), new Object())"); 187 } 188 Object objectValue2 = new Object(); 189 if (!unsafe.compareAndSwapObject(t, objectOffset, objectValue, objectValue2)) { 190 System.out.println("Unexpectedly not succeeding " + 191 "compareAndSwapObject(t, objectOffset, objectValue, objectValue2)"); 192 } 193 Object objectValue3 = new Object(); 194 if (!unsafe.compareAndSwapObject(t, objectOffset, objectValue2, objectValue3)) { 195 System.out.println("Unexpectedly not succeeding " + 196 "compareAndSwapObject(t, objectOffset, objectValue2, objectValue3)"); 197 } 198 // Exercise sun.misc.Unsafe.compareAndSwapObject using the same 199 // object (`objectValue3`) for the `expectedValue` and `newValue` arguments. 200 if (!unsafe.compareAndSwapObject(t, objectOffset, objectValue3, objectValue3)) { 201 System.out.println("Unexpectedly not succeeding " + 202 "compareAndSwapObject(t, objectOffset, objectValue3, objectValue3)"); 203 } 204 // Exercise sun.misc.Unsafe.compareAndSwapObject using the same 205 // object (`t`) for the `obj` and `newValue` arguments. 206 if (!unsafe.compareAndSwapObject(t, objectOffset, objectValue3, t)) { 207 System.out.println( 208 "Unexpectedly not succeeding compareAndSwapObject(t, objectOffset, objectValue3, t)"); 209 } 210 // Exercise sun.misc.Unsafe.compareAndSwapObject using the same 211 // object (`t`) for the `obj`, `expectedValue` and `newValue` arguments. 212 if (!unsafe.compareAndSwapObject(t, objectOffset, t, t)) { 213 System.out.println("Unexpectedly not succeeding compareAndSwapObject(t, objectOffset, t, t)"); 214 } 215 // Exercise sun.misc.Unsafe.compareAndSwapObject using the same 216 // object (`t`) for the `obj` and `expectedValue` arguments. 217 if (!unsafe.compareAndSwapObject(t, objectOffset, t, new Object())) { 218 System.out.println( 219 "Unexpectedly not succeeding compareAndSwapObject(t, objectOffset, t, new Object())"); 220 } 221 } 222 testGetAndPutVolatile(Unsafe unsafe)223 private static void testGetAndPutVolatile(Unsafe unsafe) throws NoSuchFieldException { 224 TestVolatileClass tv = new TestVolatileClass(); 225 226 int intValue = 12345678; 227 Field volatileIntField = TestVolatileClass.class.getDeclaredField("volatileIntVar"); 228 long volatileIntOffset = unsafe.objectFieldOffset(volatileIntField); 229 check(unsafe.getIntVolatile(tv, volatileIntOffset), 230 0, 231 "Unsafe.getIntVolatile(Object, long) - initial"); 232 unsafe.putIntVolatile(tv, volatileIntOffset, intValue); 233 check(tv.volatileIntVar, intValue, "Unsafe.putIntVolatile(Object, long, int)"); 234 check(unsafe.getIntVolatile(tv, volatileIntOffset), 235 intValue, 236 "Unsafe.getIntVolatile(Object, long)"); 237 238 long longValue = 1234567887654321L; 239 Field volatileLongField = TestVolatileClass.class.getDeclaredField("volatileLongVar"); 240 long volatileLongOffset = unsafe.objectFieldOffset(volatileLongField); 241 check(unsafe.getLongVolatile(tv, volatileLongOffset), 242 0, 243 "Unsafe.getLongVolatile(Object, long) - initial"); 244 unsafe.putLongVolatile(tv, volatileLongOffset, longValue); 245 check(tv.volatileLongVar, longValue, "Unsafe.putLongVolatile(Object, long, long)"); 246 check(unsafe.getLongVolatile(tv, volatileLongOffset), 247 longValue, 248 "Unsafe.getLongVolatile(Object, long)"); 249 250 Object objectValue = new Object(); 251 Field volatileObjectField = TestVolatileClass.class.getDeclaredField("volatileObjectVar"); 252 long volatileObjectOffset = unsafe.objectFieldOffset(volatileObjectField); 253 check(unsafe.getObjectVolatile(tv, volatileObjectOffset), 254 null, 255 "Unsafe.getObjectVolatile(Object, long) - initial"); 256 unsafe.putObjectVolatile(tv, volatileObjectOffset, objectValue); 257 check(tv.volatileObjectVar, objectValue, "Unsafe.putObjectVolatile(Object, long, Object)"); 258 check(unsafe.getObjectVolatile(tv, volatileObjectOffset), 259 objectValue, 260 "Unsafe.getObjectVolatile(Object, long)"); 261 } 262 testGetAndPutAbsoluteAddress(Unsafe unsafe)263 private static void testGetAndPutAbsoluteAddress(Unsafe unsafe) throws NoSuchFieldException { 264 long address = 0; 265 try { 266 address = unsafe.allocateMemory(8); 267 268 unsafe.putByte(address, (byte) 17); 269 check(unsafe.getByte(address), (byte) 17, "Unsafe.getByte(long)"); 270 271 unsafe.putInt(address, 0xDEADBEEF); 272 check(unsafe.getInt(address), 0xDEADBEEF, "Unsafe.getInt(long)"); 273 274 unsafe.putLong(address, 0xFEEDFACEDECAFBADL); 275 check(unsafe.getInt(address), 0xFEEDFACEDECAFBADL, "Unsafe.getLong(long)"); 276 } finally { 277 if (address != 0) { 278 unsafe.freeMemory(address); 279 } 280 } 281 } 282 283 // Regression test for "copyMemory" operations hitting a DCHECK() for float/double arrays. testCopyMemoryPrimitiveArrays(Unsafe unsafe)284 private static void testCopyMemoryPrimitiveArrays(Unsafe unsafe) { 285 int size = 4 * 1024; 286 long memory = unsafeTestMalloc(size); 287 288 int floatSize = 4; 289 float[] inputFloats = new float[size / floatSize]; 290 for (int i = 0; i != inputFloats.length; ++i) { 291 inputFloats[i] = ((float)i) + 0.5f; 292 } 293 float[] outputFloats = new float[size / floatSize]; 294 unsafe.copyMemoryFromPrimitiveArray(inputFloats, 0, memory, size); 295 unsafe.copyMemoryToPrimitiveArray(memory, outputFloats, 0, size); 296 for (int i = 0; i != inputFloats.length; ++i) { 297 check(inputFloats[i], outputFloats[i], "unsafe.copyMemory/float"); 298 } 299 300 int doubleSize = 8; 301 double[] inputDoubles = new double[size / doubleSize]; 302 for (int i = 0; i != inputDoubles.length; ++i) { 303 inputDoubles[i] = ((double)i) + 0.5; 304 } 305 double[] outputDoubles = new double[size / doubleSize]; 306 unsafe.copyMemoryFromPrimitiveArray(inputDoubles, 0, memory, size); 307 unsafe.copyMemoryToPrimitiveArray(memory, outputDoubles, 0, size); 308 for (int i = 0; i != inputDoubles.length; ++i) { 309 check(inputDoubles[i], outputDoubles[i], "unsafe.copyMemory/double"); 310 } 311 312 unsafeTestFree(memory); 313 } 314 315 private static class TestClass { 316 public int intVar = 0; 317 public long longVar = 0; 318 public Object objectVar = null; 319 public byte byteVar = 0; 320 } 321 322 private static class TestVolatileClass { 323 public volatile int volatileIntVar = 0; 324 public volatile long volatileLongVar = 0; 325 public volatile Object volatileObjectVar = null; 326 } 327 vmArrayBaseOffset(Class<?> clazz)328 private static native int vmArrayBaseOffset(Class<?> clazz); vmArrayIndexScale(Class<?> clazz)329 private static native int vmArrayIndexScale(Class<?> clazz); unsafeTestMalloc(long size)330 private static native long unsafeTestMalloc(long size); unsafeTestFree(long memory)331 private static native void unsafeTestFree(long memory); 332 } 333