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 { 21 static { 22 System.loadLibrary("arttest"); 23 } 24 check(int actual, int expected, String msg)25 private static void check(int actual, int expected, String msg) { 26 if (actual != expected) { 27 System.out.println(msg + " : " + actual + " != " + expected); 28 System.exit(-1); 29 } 30 } 31 check(long actual, long expected, String msg)32 private static void check(long actual, long expected, String msg) { 33 if (actual != expected) { 34 System.out.println(msg + " : " + actual + " != " + expected); 35 System.exit(-1); 36 } 37 } 38 getUnsafe()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 main(String[] args)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[])"); 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)"); 97 } 98 99 private static class TestClass { 100 public int intVar = 0; 101 public long longVar = 0; 102 } 103 vmArrayBaseOffset(Class clazz)104 private static native int vmArrayBaseOffset(Class clazz); vmArrayIndexScale(Class clazz)105 private static native int vmArrayIndexScale(Class clazz); 106 } 107