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 Exception { 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 Exception { 50 System.loadLibrary(args[0]); 51 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[])"); 85 86 TestClass t = new TestClass(); 87 88 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)"); 95 96 long longValue = 1234567887654321L; 97 Field longField = TestClass.class.getDeclaredField("longVar"); 98 long longOffset = unsafe.objectFieldOffset(longField); 99 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)"); 103 104 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 112 if (unsafe.compareAndSwapInt(t, intOffset, 0, 1)) { 113 System.out.println("Unexpectedly succeeding compareAndSwapInt(t, intOffset, 0, 1)"); 114 } 115 if (!unsafe.compareAndSwapInt(t, intOffset, intValue, 0)) { 116 System.out.println( 117 "Unexpectedly not succeeding compareAndSwapInt(t, intOffset, intValue, 0)"); 118 } 119 if (!unsafe.compareAndSwapInt(t, intOffset, 0, 1)) { 120 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)"); 126 } 127 128 if (unsafe.compareAndSwapLong(t, longOffset, 0, 1)) { 129 System.out.println("Unexpectedly succeeding compareAndSwapLong(t, longOffset, 0, 1)"); 130 } 131 if (!unsafe.compareAndSwapLong(t, longOffset, longValue, 0)) { 132 System.out.println( 133 "Unexpectedly not succeeding compareAndSwapLong(t, longOffset, longValue, 0)"); 134 } 135 if (!unsafe.compareAndSwapLong(t, longOffset, 0, 1)) { 136 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)"); 142 } 143 144 // 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())) { 150 System.out.println("Unexpectedly succeeding " + 151 "compareAndSwapObject(t, objectOffset, new Object(), new Object())"); 152 } 153 Object objectValue2 = new Object(); 154 if (!unsafe.compareAndSwapObject(t, objectOffset, objectValue, objectValue2)) { 155 System.out.println("Unexpectedly not succeeding " + 156 "compareAndSwapObject(t, objectOffset, objectValue, objectValue2)"); 157 } 158 Object objectValue3 = new Object(); 159 if (!unsafe.compareAndSwapObject(t, objectOffset, objectValue2, objectValue3)) { 160 System.out.println("Unexpectedly not succeeding " + 161 "compareAndSwapObject(t, objectOffset, objectValue2, objectValue3)"); 162 } 163 // 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 } 169 // 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)) { 172 System.out.println( 173 "Unexpectedly not succeeding compareAndSwapObject(t, objectOffset, objectValue3, t)"); 174 } 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)) { 178 System.out.println("Unexpectedly not succeeding compareAndSwapObject(t, objectOffset, t, t)"); 179 } 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())) { 183 System.out.println( 184 "Unexpectedly not succeeding compareAndSwapObject(t, objectOffset, t, new Object())"); 185 } 186 } 187 188 private static class TestClass { 189 public int intVar = 0; 190 public long longVar = 0; 191 public Object objectVar = null; 192 } 193 vmArrayBaseOffset(Class clazz)194 private static native int vmArrayBaseOffset(Class clazz); vmArrayIndexScale(Class clazz)195 private static native int vmArrayIndexScale(Class clazz); 196 } 197