Hiroshi Yamauchi | 4d2efce | 2014-02-10 16:19:09 -0800 | [diff] [blame] | 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 | |
Andreas Gampe | 1c83cbc | 2014-07-22 18:52:29 -0700 | [diff] [blame] | 20 | public class Main { |
Hiroshi Yamauchi | 4d2efce | 2014-02-10 16:19:09 -0800 | [diff] [blame] | 21 | private static void check(int actual, int expected, String msg) { |
| 22 | if (actual != expected) { |
Andreas Gampe | 1c83cbc | 2014-07-22 18:52:29 -0700 | [diff] [blame] | 23 | System.out.println(msg + " : " + actual + " != " + expected); |
Brian Carlstrom | da4442e | 2014-10-14 15:39:01 -0700 | [diff] [blame] | 24 | System.exit(1); |
Hiroshi Yamauchi | 4d2efce | 2014-02-10 16:19:09 -0800 | [diff] [blame] | 25 | } |
| 26 | } |
| 27 | |
Vladimir Marko | 99f391e | 2014-04-03 12:56:06 +0100 | [diff] [blame] | 28 | private static void check(long actual, long expected, String msg) { |
| 29 | if (actual != expected) { |
Andreas Gampe | 1c83cbc | 2014-07-22 18:52:29 -0700 | [diff] [blame] | 30 | System.out.println(msg + " : " + actual + " != " + expected); |
Brian Carlstrom | da4442e | 2014-10-14 15:39:01 -0700 | [diff] [blame] | 31 | System.exit(1); |
Vladimir Marko | 99f391e | 2014-04-03 12:56:06 +0100 | [diff] [blame] | 32 | } |
| 33 | } |
| 34 | |
Roland Levillain | ca1476f | 2015-06-16 18:09:26 +0100 | [diff] [blame] | 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 | |
Roland Levillain | 3d31242 | 2016-06-23 13:53:42 +0100 | [diff] [blame] | 42 | private static Unsafe getUnsafe() throws NoSuchFieldException, IllegalAccessException { |
Aart Bik | 0e54c01 | 2016-03-04 12:08:31 -0800 | [diff] [blame] | 43 | Class<?> unsafeClass = Unsafe.class; |
Hiroshi Yamauchi | 4d2efce | 2014-02-10 16:19:09 -0800 | [diff] [blame] | 44 | Field f = unsafeClass.getDeclaredField("theUnsafe"); |
| 45 | f.setAccessible(true); |
| 46 | return (Unsafe) f.get(null); |
| 47 | } |
| 48 | |
Roland Levillain | 3d31242 | 2016-06-23 13:53:42 +0100 | [diff] [blame] | 49 | public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException { |
Mathieu Chartier | 031768a | 2015-08-27 10:25:02 -0700 | [diff] [blame] | 50 | System.loadLibrary(args[0]); |
Hiroshi Yamauchi | 4d2efce | 2014-02-10 16:19:09 -0800 | [diff] [blame] | 51 | Unsafe unsafe = getUnsafe(); |
Roland Levillain | 3d31242 | 2016-06-23 13:53:42 +0100 | [diff] [blame] | 52 | |
| 53 | testArrayBaseOffset(unsafe); |
| 54 | testArrayIndexScale(unsafe); |
| 55 | testGetAndPutAndCAS(unsafe); |
| 56 | testGetAndPutVolatile(unsafe); |
| 57 | } |
| 58 | |
| 59 | private static void testArrayBaseOffset(Unsafe unsafe) { |
Hiroshi Yamauchi | 4d2efce | 2014-02-10 16:19:09 -0800 | [diff] [blame] | 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[])"); |
Roland Levillain | 3d31242 | 2016-06-23 13:53:42 +0100 | [diff] [blame] | 76 | } |
Hiroshi Yamauchi | 4d2efce | 2014-02-10 16:19:09 -0800 | [diff] [blame] | 77 | |
Roland Levillain | 3d31242 | 2016-06-23 13:53:42 +0100 | [diff] [blame] | 78 | private static void testArrayIndexScale(Unsafe unsafe) { |
Hiroshi Yamauchi | 4d2efce | 2014-02-10 16:19:09 -0800 | [diff] [blame] | 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[])"); |
Roland Levillain | 3d31242 | 2016-06-23 13:53:42 +0100 | [diff] [blame] | 95 | } |
Vladimir Marko | 99f391e | 2014-04-03 12:56:06 +0100 | [diff] [blame] | 96 | |
Roland Levillain | 3d31242 | 2016-06-23 13:53:42 +0100 | [diff] [blame] | 97 | private static void testGetAndPutAndCAS(Unsafe unsafe) throws NoSuchFieldException { |
Vladimir Marko | 99f391e | 2014-04-03 12:56:06 +0100 | [diff] [blame] | 98 | TestClass t = new TestClass(); |
Roland Levillain | ca1476f | 2015-06-16 18:09:26 +0100 | [diff] [blame] | 99 | |
Vladimir Marko | 99f391e | 2014-04-03 12:56:06 +0100 | [diff] [blame] | 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)"); |
Roland Levillain | ca1476f | 2015-06-16 18:09:26 +0100 | [diff] [blame] | 107 | |
| 108 | long longValue = 1234567887654321L; |
Vladimir Marko | 99f391e | 2014-04-03 12:56:06 +0100 | [diff] [blame] | 109 | Field longField = TestClass.class.getDeclaredField("longVar"); |
| 110 | long longOffset = unsafe.objectFieldOffset(longField); |
Vladimir Marko | 99f391e | 2014-04-03 12:56:06 +0100 | [diff] [blame] | 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)"); |
Andreas Gampe | 2bcf9bf | 2015-01-29 09:56:07 -0800 | [diff] [blame] | 115 | |
Roland Levillain | ca1476f | 2015-06-16 18:09:26 +0100 | [diff] [blame] | 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 | |
Andreas Gampe | 2bcf9bf | 2015-01-29 09:56:07 -0800 | [diff] [blame] | 124 | if (unsafe.compareAndSwapInt(t, intOffset, 0, 1)) { |
Roland Levillain | 2e50ecb | 2016-01-27 14:08:33 +0000 | [diff] [blame] | 125 | System.out.println("Unexpectedly succeeding compareAndSwapInt(t, intOffset, 0, 1)"); |
Andreas Gampe | 2bcf9bf | 2015-01-29 09:56:07 -0800 | [diff] [blame] | 126 | } |
| 127 | if (!unsafe.compareAndSwapInt(t, intOffset, intValue, 0)) { |
Roland Levillain | 2e50ecb | 2016-01-27 14:08:33 +0000 | [diff] [blame] | 128 | System.out.println( |
| 129 | "Unexpectedly not succeeding compareAndSwapInt(t, intOffset, intValue, 0)"); |
Andreas Gampe | 2bcf9bf | 2015-01-29 09:56:07 -0800 | [diff] [blame] | 130 | } |
| 131 | if (!unsafe.compareAndSwapInt(t, intOffset, 0, 1)) { |
Roland Levillain | 2e50ecb | 2016-01-27 14:08:33 +0000 | [diff] [blame] | 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)"); |
Andreas Gampe | 2bcf9bf | 2015-01-29 09:56:07 -0800 | [diff] [blame] | 138 | } |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 139 | |
| 140 | if (unsafe.compareAndSwapLong(t, longOffset, 0, 1)) { |
Roland Levillain | 2e50ecb | 2016-01-27 14:08:33 +0000 | [diff] [blame] | 141 | System.out.println("Unexpectedly succeeding compareAndSwapLong(t, longOffset, 0, 1)"); |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 142 | } |
| 143 | if (!unsafe.compareAndSwapLong(t, longOffset, longValue, 0)) { |
Roland Levillain | 2e50ecb | 2016-01-27 14:08:33 +0000 | [diff] [blame] | 144 | System.out.println( |
| 145 | "Unexpectedly not succeeding compareAndSwapLong(t, longOffset, longValue, 0)"); |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 146 | } |
| 147 | if (!unsafe.compareAndSwapLong(t, longOffset, 0, 1)) { |
Roland Levillain | 2e50ecb | 2016-01-27 14:08:33 +0000 | [diff] [blame] | 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)"); |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 154 | } |
Roland Levillain | b550c2e | 2015-06-26 10:44:53 +0100 | [diff] [blame] | 155 | |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 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())) { |
Roland Levillain | 2e50ecb | 2016-01-27 14:08:33 +0000 | [diff] [blame] | 162 | System.out.println("Unexpectedly succeeding " + |
| 163 | "compareAndSwapObject(t, objectOffset, new Object(), new Object())"); |
Roland Levillain | b550c2e | 2015-06-26 10:44:53 +0100 | [diff] [blame] | 164 | } |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 165 | Object objectValue2 = new Object(); |
| 166 | if (!unsafe.compareAndSwapObject(t, objectOffset, objectValue, objectValue2)) { |
Roland Levillain | 2e50ecb | 2016-01-27 14:08:33 +0000 | [diff] [blame] | 167 | System.out.println("Unexpectedly not succeeding " + |
| 168 | "compareAndSwapObject(t, objectOffset, objectValue, objectValue2)"); |
Roland Levillain | b550c2e | 2015-06-26 10:44:53 +0100 | [diff] [blame] | 169 | } |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 170 | Object objectValue3 = new Object(); |
| 171 | if (!unsafe.compareAndSwapObject(t, objectOffset, objectValue2, objectValue3)) { |
Roland Levillain | 2e50ecb | 2016-01-27 14:08:33 +0000 | [diff] [blame] | 172 | System.out.println("Unexpectedly not succeeding " + |
| 173 | "compareAndSwapObject(t, objectOffset, objectValue2, objectValue3)"); |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 174 | } |
Roland Levillain | 2e50ecb | 2016-01-27 14:08:33 +0000 | [diff] [blame] | 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 | } |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 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)) { |
Roland Levillain | 2e50ecb | 2016-01-27 14:08:33 +0000 | [diff] [blame] | 184 | System.out.println( |
| 185 | "Unexpectedly not succeeding compareAndSwapObject(t, objectOffset, objectValue3, t)"); |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 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)) { |
Roland Levillain | 2e50ecb | 2016-01-27 14:08:33 +0000 | [diff] [blame] | 190 | System.out.println("Unexpectedly not succeeding compareAndSwapObject(t, objectOffset, t, t)"); |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 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())) { |
Roland Levillain | 2e50ecb | 2016-01-27 14:08:33 +0000 | [diff] [blame] | 195 | System.out.println( |
| 196 | "Unexpectedly not succeeding compareAndSwapObject(t, objectOffset, t, new Object())"); |
Roland Levillain | b550c2e | 2015-06-26 10:44:53 +0100 | [diff] [blame] | 197 | } |
Vladimir Marko | 99f391e | 2014-04-03 12:56:06 +0100 | [diff] [blame] | 198 | } |
| 199 | |
Roland Levillain | 3d31242 | 2016-06-23 13:53:42 +0100 | [diff] [blame] | 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 | |
Vladimir Marko | 99f391e | 2014-04-03 12:56:06 +0100 | [diff] [blame] | 240 | private static class TestClass { |
| 241 | public int intVar = 0; |
| 242 | public long longVar = 0; |
Roland Levillain | ca1476f | 2015-06-16 18:09:26 +0100 | [diff] [blame] | 243 | public Object objectVar = null; |
Hiroshi Yamauchi | 4d2efce | 2014-02-10 16:19:09 -0800 | [diff] [blame] | 244 | } |
| 245 | |
Roland Levillain | 3d31242 | 2016-06-23 13:53:42 +0100 | [diff] [blame] | 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 | |
Andreas Gampe | 166aaee | 2016-07-18 08:27:23 -0700 | [diff] [blame] | 252 | private static native int vmArrayBaseOffset(Class<?> clazz); |
| 253 | private static native int vmArrayIndexScale(Class<?> clazz); |
Hiroshi Yamauchi | 4d2efce | 2014-02-10 16:19:09 -0800 | [diff] [blame] | 254 | } |