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 | static { |
| 22 | System.loadLibrary("arttest"); |
| 23 | } |
| 24 | |
| 25 | private static void check(int actual, int expected, String msg) { |
| 26 | if (actual != expected) { |
Andreas Gampe | 1c83cbc | 2014-07-22 18:52:29 -0700 | [diff] [blame] | 27 | System.out.println(msg + " : " + actual + " != " + expected); |
Brian Carlstrom | da4442e | 2014-10-14 15:39:01 -0700 | [diff] [blame] | 28 | System.exit(1); |
Hiroshi Yamauchi | 4d2efce | 2014-02-10 16:19:09 -0800 | [diff] [blame] | 29 | } |
| 30 | } |
| 31 | |
Vladimir Marko | 99f391e | 2014-04-03 12:56:06 +0100 | [diff] [blame] | 32 | private static void check(long actual, long expected, String msg) { |
| 33 | if (actual != expected) { |
Andreas Gampe | 1c83cbc | 2014-07-22 18:52:29 -0700 | [diff] [blame] | 34 | System.out.println(msg + " : " + actual + " != " + expected); |
Brian Carlstrom | da4442e | 2014-10-14 15:39:01 -0700 | [diff] [blame] | 35 | System.exit(1); |
Vladimir Marko | 99f391e | 2014-04-03 12:56:06 +0100 | [diff] [blame] | 36 | } |
| 37 | } |
| 38 | |
Hiroshi Yamauchi | 4d2efce | 2014-02-10 16:19:09 -0800 | [diff] [blame] | 39 | private static Unsafe getUnsafe() throws Exception { |
| 40 | Class<?> unsafeClass = Class.forName("sun.misc.Unsafe"); |
| 41 | Field f = unsafeClass.getDeclaredField("theUnsafe"); |
| 42 | f.setAccessible(true); |
| 43 | return (Unsafe) f.get(null); |
| 44 | } |
| 45 | |
| 46 | public static void main(String[] args) throws Exception { |
| 47 | Unsafe unsafe = getUnsafe(); |
| 48 | check(unsafe.arrayBaseOffset(boolean[].class), vmArrayBaseOffset(boolean[].class), |
| 49 | "Unsafe.arrayBaseOffset(boolean[])"); |
| 50 | check(unsafe.arrayBaseOffset(byte[].class), vmArrayBaseOffset(byte[].class), |
| 51 | "Unsafe.arrayBaseOffset(byte[])"); |
| 52 | check(unsafe.arrayBaseOffset(char[].class), vmArrayBaseOffset(char[].class), |
| 53 | "Unsafe.arrayBaseOffset(char[])"); |
| 54 | check(unsafe.arrayBaseOffset(double[].class), vmArrayBaseOffset(double[].class), |
| 55 | "Unsafe.arrayBaseOffset(double[])"); |
| 56 | check(unsafe.arrayBaseOffset(float[].class), vmArrayBaseOffset(float[].class), |
| 57 | "Unsafe.arrayBaseOffset(float[])"); |
| 58 | check(unsafe.arrayBaseOffset(int[].class), vmArrayBaseOffset(int[].class), |
| 59 | "Unsafe.arrayBaseOffset(int[])"); |
| 60 | check(unsafe.arrayBaseOffset(long[].class), vmArrayBaseOffset(long[].class), |
| 61 | "Unsafe.arrayBaseOffset(long[])"); |
| 62 | check(unsafe.arrayBaseOffset(Object[].class), vmArrayBaseOffset(Object[].class), |
| 63 | "Unsafe.arrayBaseOffset(Object[])"); |
| 64 | |
| 65 | check(unsafe.arrayIndexScale(boolean[].class), vmArrayIndexScale(boolean[].class), |
| 66 | "Unsafe.arrayIndexScale(boolean[])"); |
| 67 | check(unsafe.arrayIndexScale(byte[].class), vmArrayIndexScale(byte[].class), |
| 68 | "Unsafe.arrayIndexScale(byte[])"); |
| 69 | check(unsafe.arrayIndexScale(char[].class), vmArrayIndexScale(char[].class), |
| 70 | "Unsafe.arrayIndexScale(char[])"); |
| 71 | check(unsafe.arrayIndexScale(double[].class), vmArrayIndexScale(double[].class), |
| 72 | "Unsafe.arrayIndexScale(double[])"); |
| 73 | check(unsafe.arrayIndexScale(float[].class), vmArrayIndexScale(float[].class), |
| 74 | "Unsafe.arrayIndexScale(float[])"); |
| 75 | check(unsafe.arrayIndexScale(int[].class), vmArrayIndexScale(int[].class), |
| 76 | "Unsafe.arrayIndexScale(int[])"); |
| 77 | check(unsafe.arrayIndexScale(long[].class), vmArrayIndexScale(long[].class), |
| 78 | "Unsafe.arrayIndexScale(long[])"); |
| 79 | check(unsafe.arrayIndexScale(Object[].class), vmArrayIndexScale(Object[].class), |
| 80 | "Unsafe.arrayIndexScale(Object[])"); |
Vladimir Marko | 99f391e | 2014-04-03 12:56:06 +0100 | [diff] [blame] | 81 | |
| 82 | TestClass t = new TestClass(); |
| 83 | int intValue = 12345678; |
| 84 | Field intField = TestClass.class.getDeclaredField("intVar"); |
| 85 | long intOffset = unsafe.objectFieldOffset(intField); |
| 86 | check(unsafe.getInt(t, intOffset), 0, "Unsafe.getInt(Object, long) - initial"); |
| 87 | unsafe.putInt(t, intOffset, intValue); |
| 88 | check(t.intVar, intValue, "Unsafe.putInt(Object, long, int)"); |
| 89 | check(unsafe.getInt(t, intOffset), intValue, "Unsafe.getInt(Object, long)"); |
| 90 | Field longField = TestClass.class.getDeclaredField("longVar"); |
| 91 | long longOffset = unsafe.objectFieldOffset(longField); |
| 92 | long longValue = 1234567887654321L; |
| 93 | check(unsafe.getLong(t, longOffset), 0, "Unsafe.getLong(Object, long) - initial"); |
| 94 | unsafe.putLong(t, longOffset, longValue); |
| 95 | check(t.longVar, longValue, "Unsafe.putLong(Object, long, long)"); |
| 96 | check(unsafe.getLong(t, longOffset), longValue, "Unsafe.getLong(Object, long)"); |
Andreas Gampe | 2bcf9bf | 2015-01-29 09:56:07 -0800 | [diff] [blame] | 97 | |
| 98 | if (unsafe.compareAndSwapInt(t, intOffset, 0, 1)) { |
| 99 | System.out.println("Unexpectedly succeeding compareAndSwap..."); |
| 100 | } |
| 101 | if (!unsafe.compareAndSwapInt(t, intOffset, intValue, 0)) { |
| 102 | System.out.println("Unexpectedly not succeeding compareAndSwap..."); |
| 103 | } |
| 104 | if (!unsafe.compareAndSwapInt(t, intOffset, 0, 1)) { |
| 105 | System.out.println("Unexpectedly not succeeding compareAndSwap..."); |
| 106 | } |
Vladimir Marko | 99f391e | 2014-04-03 12:56:06 +0100 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | private static class TestClass { |
| 110 | public int intVar = 0; |
| 111 | public long longVar = 0; |
Hiroshi Yamauchi | 4d2efce | 2014-02-10 16:19:09 -0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | private static native int vmArrayBaseOffset(Class clazz); |
| 115 | private static native int vmArrayIndexScale(Class clazz); |
| 116 | } |