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 | |
Hiroshi Yamauchi | 4d2efce | 2014-02-10 16:19:09 -0800 | [diff] [blame] | 42 | private static Unsafe getUnsafe() throws Exception { |
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 | |
| 49 | public static void main(String[] args) throws Exception { |
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(); |
| 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[])"); |
Vladimir Marko | 99f391e | 2014-04-03 12:56:06 +0100 | [diff] [blame] | 85 | |
| 86 | TestClass t = new TestClass(); |
Roland Levillain | ca1476f | 2015-06-16 18:09:26 +0100 | [diff] [blame] | 87 | |
Vladimir Marko | 99f391e | 2014-04-03 12:56:06 +0100 | [diff] [blame] | 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)"); |
Roland Levillain | ca1476f | 2015-06-16 18:09:26 +0100 | [diff] [blame] | 95 | |
| 96 | long longValue = 1234567887654321L; |
Vladimir Marko | 99f391e | 2014-04-03 12:56:06 +0100 | [diff] [blame] | 97 | Field longField = TestClass.class.getDeclaredField("longVar"); |
| 98 | long longOffset = unsafe.objectFieldOffset(longField); |
Vladimir Marko | 99f391e | 2014-04-03 12:56:06 +0100 | [diff] [blame] | 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)"); |
Andreas Gampe | 2bcf9bf | 2015-01-29 09:56:07 -0800 | [diff] [blame] | 103 | |
Roland Levillain | ca1476f | 2015-06-16 18:09:26 +0100 | [diff] [blame] | 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 | |
Andreas Gampe | 2bcf9bf | 2015-01-29 09:56:07 -0800 | [diff] [blame] | 112 | if (unsafe.compareAndSwapInt(t, intOffset, 0, 1)) { |
Roland Levillain | 2e50ecb | 2016-01-27 14:08:33 +0000 | [diff] [blame] | 113 | System.out.println("Unexpectedly succeeding compareAndSwapInt(t, intOffset, 0, 1)"); |
Andreas Gampe | 2bcf9bf | 2015-01-29 09:56:07 -0800 | [diff] [blame] | 114 | } |
| 115 | if (!unsafe.compareAndSwapInt(t, intOffset, intValue, 0)) { |
Roland Levillain | 2e50ecb | 2016-01-27 14:08:33 +0000 | [diff] [blame] | 116 | System.out.println( |
| 117 | "Unexpectedly not succeeding compareAndSwapInt(t, intOffset, intValue, 0)"); |
Andreas Gampe | 2bcf9bf | 2015-01-29 09:56:07 -0800 | [diff] [blame] | 118 | } |
| 119 | if (!unsafe.compareAndSwapInt(t, intOffset, 0, 1)) { |
Roland Levillain | 2e50ecb | 2016-01-27 14:08:33 +0000 | [diff] [blame] | 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)"); |
Andreas Gampe | 2bcf9bf | 2015-01-29 09:56:07 -0800 | [diff] [blame] | 126 | } |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 127 | |
| 128 | if (unsafe.compareAndSwapLong(t, longOffset, 0, 1)) { |
Roland Levillain | 2e50ecb | 2016-01-27 14:08:33 +0000 | [diff] [blame] | 129 | System.out.println("Unexpectedly succeeding compareAndSwapLong(t, longOffset, 0, 1)"); |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 130 | } |
| 131 | if (!unsafe.compareAndSwapLong(t, longOffset, longValue, 0)) { |
Roland Levillain | 2e50ecb | 2016-01-27 14:08:33 +0000 | [diff] [blame] | 132 | System.out.println( |
| 133 | "Unexpectedly not succeeding compareAndSwapLong(t, longOffset, longValue, 0)"); |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 134 | } |
| 135 | if (!unsafe.compareAndSwapLong(t, longOffset, 0, 1)) { |
Roland Levillain | 2e50ecb | 2016-01-27 14:08:33 +0000 | [diff] [blame] | 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)"); |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 142 | } |
Roland Levillain | b550c2e | 2015-06-26 10:44:53 +0100 | [diff] [blame] | 143 | |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 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())) { |
Roland Levillain | 2e50ecb | 2016-01-27 14:08:33 +0000 | [diff] [blame] | 150 | System.out.println("Unexpectedly succeeding " + |
| 151 | "compareAndSwapObject(t, objectOffset, new Object(), new Object())"); |
Roland Levillain | b550c2e | 2015-06-26 10:44:53 +0100 | [diff] [blame] | 152 | } |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 153 | Object objectValue2 = new Object(); |
| 154 | if (!unsafe.compareAndSwapObject(t, objectOffset, objectValue, objectValue2)) { |
Roland Levillain | 2e50ecb | 2016-01-27 14:08:33 +0000 | [diff] [blame] | 155 | System.out.println("Unexpectedly not succeeding " + |
| 156 | "compareAndSwapObject(t, objectOffset, objectValue, objectValue2)"); |
Roland Levillain | b550c2e | 2015-06-26 10:44:53 +0100 | [diff] [blame] | 157 | } |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 158 | Object objectValue3 = new Object(); |
| 159 | if (!unsafe.compareAndSwapObject(t, objectOffset, objectValue2, objectValue3)) { |
Roland Levillain | 2e50ecb | 2016-01-27 14:08:33 +0000 | [diff] [blame] | 160 | System.out.println("Unexpectedly not succeeding " + |
| 161 | "compareAndSwapObject(t, objectOffset, objectValue2, objectValue3)"); |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 162 | } |
Roland Levillain | 2e50ecb | 2016-01-27 14:08:33 +0000 | [diff] [blame] | 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 | } |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 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)) { |
Roland Levillain | 2e50ecb | 2016-01-27 14:08:33 +0000 | [diff] [blame] | 172 | System.out.println( |
| 173 | "Unexpectedly not succeeding compareAndSwapObject(t, objectOffset, objectValue3, t)"); |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 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)) { |
Roland Levillain | 2e50ecb | 2016-01-27 14:08:33 +0000 | [diff] [blame] | 178 | System.out.println("Unexpectedly not succeeding compareAndSwapObject(t, objectOffset, t, t)"); |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 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())) { |
Roland Levillain | 2e50ecb | 2016-01-27 14:08:33 +0000 | [diff] [blame] | 183 | System.out.println( |
| 184 | "Unexpectedly not succeeding compareAndSwapObject(t, objectOffset, t, new Object())"); |
Roland Levillain | b550c2e | 2015-06-26 10:44:53 +0100 | [diff] [blame] | 185 | } |
Vladimir Marko | 99f391e | 2014-04-03 12:56:06 +0100 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | private static class TestClass { |
| 189 | public int intVar = 0; |
| 190 | public long longVar = 0; |
Roland Levillain | ca1476f | 2015-06-16 18:09:26 +0100 | [diff] [blame] | 191 | public Object objectVar = null; |
Hiroshi Yamauchi | 4d2efce | 2014-02-10 16:19:09 -0800 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | private static native int vmArrayBaseOffset(Class clazz); |
| 195 | private static native int vmArrayIndexScale(Class clazz); |
| 196 | } |