blob: 9e2ff876b718e73c87687cae7804bee996f67771 [file] [log] [blame]
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -08001/*
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
17import java.lang.reflect.Field;
18import sun.misc.Unsafe;
19
20public class UnsafeTest {
21 static {
22 System.loadLibrary("arttest");
23 }
24
25 private static void check(int actual, int expected, String msg) {
26 if (actual != expected) {
27 System.logE(msg + " : " + actual + " != " + expected);
28 System.exit(-1);
29 }
30 }
31
Vladimir Marko99f391e2014-04-03 12:56:06 +010032 private static void check(long actual, long expected, String msg) {
33 if (actual != expected) {
34 System.logE(msg + " : " + actual + " != " + expected);
35 System.exit(-1);
36 }
37 }
38
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -080039 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 Marko99f391e2014-04-03 12:56:06 +010081
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;
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -0800102 }
103
104 private static native int vmArrayBaseOffset(Class clazz);
105 private static native int vmArrayIndexScale(Class clazz);
106}