blob: bb098e44dc6a872b4adeb9a3bcf42f56c778bc1f [file] [log] [blame]
Brian Carlstromce888532013-10-10 00:32:58 -07001/*
2 * Copyright (C) 2013 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
Mathieu Chartier72156e22015-07-10 18:26:41 -070017import java.lang.reflect.InvocationHandler;
Jeff Hao201803f2013-11-20 18:11:39 -080018import java.lang.reflect.Method;
Mathieu Chartier72156e22015-07-10 18:26:41 -070019import java.lang.reflect.Proxy;
Jeff Hao201803f2013-11-20 18:11:39 -080020
Igor Murashkin367f3dd2016-09-01 17:00:24 -070021import dalvik.annotation.optimization.CriticalNative;
Igor Murashkin9d4b6da2016-07-29 09:51:58 -070022import dalvik.annotation.optimization.FastNative;
23
Andreas Gampe1c83cbc2014-07-22 18:52:29 -070024public class Main {
Brian Carlstromce888532013-10-10 00:32:58 -070025 public static void main(String[] args) {
Mathieu Chartier031768a2015-08-27 10:25:02 -070026 System.loadLibrary(args[0]);
Brian Carlstromce888532013-10-10 00:32:58 -070027 testFindClassOnAttachedNativeThread();
Jeff Hao62509b62013-12-10 17:44:56 -080028 testFindFieldOnAttachedNativeThread();
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +010029 testReflectFieldGetFromAttachedNativeThreadNative();
Brian Carlstrom67fe2b42013-10-15 18:51:42 -070030 testCallStaticVoidMethodOnSubClass();
Jeff Hao201803f2013-11-20 18:11:39 -080031 testGetMirandaMethod();
Narayan Kamathef809d02013-12-19 17:52:47 +000032 testZeroLengthByteBuffers();
Andreas Gamped1104322014-05-01 14:38:56 -070033 testByteMethod();
34 testShortMethod();
35 testBooleanMethod();
36 testCharMethod();
Narayan Kamath1268b742014-07-11 19:15:11 +010037 testIsAssignableFromOnPrimitiveTypes();
Andreas Gampe718ac652014-08-11 18:51:53 -070038 testShallowGetCallingClassLoader();
Andreas Gampe0d334ce2014-08-13 23:05:38 -070039 testShallowGetStackClass2();
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -070040 testCallNonvirtual();
Jeff Hao848f70a2014-01-15 13:49:50 -080041 testNewStringObject();
Mathieu Chartierff6d8cf2015-06-02 13:40:12 -070042 testRemoveLocalObject();
Mathieu Chartier72156e22015-07-10 18:26:41 -070043 testProxyGetMethodID();
Hiroshi Yamauchi20a0be02016-02-19 15:44:06 -080044 testJniCriticalSectionAndGc();
Alex Light36121492016-02-22 13:43:29 -080045 testCallDefaultMethods();
Alex Lighte9d2ca22016-02-25 16:13:54 -080046 String lambda = "λ";
47 testInvokeLambdaMethod(() -> { System.out.println("hi-lambda: " + lambda); });
48 String def = "δ";
49 testInvokeLambdaDefaultMethod(() -> { System.out.println("hi-default " + def + lambda); });
Igor Murashkin9d4b6da2016-07-29 09:51:58 -070050
51 registerNativesJniTest();
52 testFastNativeMethods();
Igor Murashkin367f3dd2016-09-01 17:00:24 -070053 testCriticalNativeMethods();
Brian Carlstromce888532013-10-10 00:32:58 -070054 }
55
Igor Murashkin9d4b6da2016-07-29 09:51:58 -070056 private static native boolean registerNativesJniTest();
57
Alex Light36121492016-02-22 13:43:29 -080058 private static native void testCallDefaultMethods();
59
Brian Carlstromce888532013-10-10 00:32:58 -070060 private static native void testFindClassOnAttachedNativeThread();
Brian Carlstrom67fe2b42013-10-15 18:51:42 -070061
Jeff Hao62509b62013-12-10 17:44:56 -080062 private static boolean testFindFieldOnAttachedNativeThreadField;
63
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +010064 private static native void testReflectFieldGetFromAttachedNativeThreadNative();
65
66 public static boolean testReflectFieldGetFromAttachedNativeThreadField;
67
Jeff Hao62509b62013-12-10 17:44:56 -080068 private static void testFindFieldOnAttachedNativeThread() {
69 testFindFieldOnAttachedNativeThreadNative();
70 if (!testFindFieldOnAttachedNativeThreadField) {
71 throw new AssertionError();
72 }
73 }
74
75 private static native void testFindFieldOnAttachedNativeThreadNative();
76
Brian Carlstrom67fe2b42013-10-15 18:51:42 -070077 private static void testCallStaticVoidMethodOnSubClass() {
78 testCallStaticVoidMethodOnSubClassNative();
79 if (!testCallStaticVoidMethodOnSubClass_SuperClass.executed) {
80 throw new AssertionError();
81 }
82 }
83
84 private static native void testCallStaticVoidMethodOnSubClassNative();
85
86 private static class testCallStaticVoidMethodOnSubClass_SuperClass {
87 private static boolean executed = false;
88 private static void execute() {
89 executed = true;
90 }
91 }
92
93 private static class testCallStaticVoidMethodOnSubClass_SubClass
94 extends testCallStaticVoidMethodOnSubClass_SuperClass {
95 }
Jeff Hao201803f2013-11-20 18:11:39 -080096
97 private static native Method testGetMirandaMethodNative();
98
99 private static void testGetMirandaMethod() {
100 Method m = testGetMirandaMethodNative();
101 if (m.getDeclaringClass() != testGetMirandaMethod_MirandaInterface.class) {
102 throw new AssertionError();
103 }
104 }
105
Narayan Kamathef809d02013-12-19 17:52:47 +0000106 private static native void testZeroLengthByteBuffers();
107
Jeff Hao201803f2013-11-20 18:11:39 -0800108 private static abstract class testGetMirandaMethod_MirandaAbstract implements testGetMirandaMethod_MirandaInterface {
109 public boolean inAbstract() {
110 return true;
111 }
112 }
113
114 private static interface testGetMirandaMethod_MirandaInterface {
115 public boolean inInterface();
116 }
Andreas Gamped1104322014-05-01 14:38:56 -0700117
118 // Test sign-extension for values < 32b
119
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700120 static native byte byteMethod(byte b1, byte b2, byte b3, byte b4, byte b5, byte b6, byte b7,
Andreas Gamped1104322014-05-01 14:38:56 -0700121 byte b8, byte b9, byte b10);
122
123 private static void testByteMethod() {
124 byte returns[] = { 0, 1, 2, 127, -1, -2, -128 };
125 for (int i = 0; i < returns.length; i++) {
126 byte result = byteMethod((byte)i, (byte)2, (byte)(-3), (byte)4, (byte)(-5), (byte)6,
127 (byte)(-7), (byte)8, (byte)(-9), (byte)10);
128 if (returns[i] != result) {
129 System.out.println("Run " + i + " with " + returns[i] + " vs " + result);
130 throw new AssertionError();
131 }
132 }
133 }
134
Mathieu Chartierff6d8cf2015-06-02 13:40:12 -0700135 private static native void removeLocalObject(Object o);
136
137 private static void testRemoveLocalObject() {
138 removeLocalObject(new Object());
139 }
Alex Light36121492016-02-22 13:43:29 -0800140
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700141 private static native short shortMethod(short s1, short s2, short s3, short s4, short s5, short s6, short s7,
Andreas Gamped1104322014-05-01 14:38:56 -0700142 short s8, short s9, short s10);
143
144 private static void testShortMethod() {
145 short returns[] = { 0, 1, 2, 127, 32767, -1, -2, -128, -32768 };
146 for (int i = 0; i < returns.length; i++) {
147 short result = shortMethod((short)i, (short)2, (short)(-3), (short)4, (short)(-5), (short)6,
148 (short)(-7), (short)8, (short)(-9), (short)10);
149 if (returns[i] != result) {
150 System.out.println("Run " + i + " with " + returns[i] + " vs " + result);
151 throw new AssertionError();
152 }
153 }
154 }
155
156 // Test zero-extension for values < 32b
157
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700158 private static native boolean booleanMethod(boolean b1, boolean b2, boolean b3, boolean b4, boolean b5, boolean b6, boolean b7,
Andreas Gamped1104322014-05-01 14:38:56 -0700159 boolean b8, boolean b9, boolean b10);
160
161 private static void testBooleanMethod() {
162 if (booleanMethod(false, true, false, true, false, true, false, true, false, true)) {
163 throw new AssertionError();
164 }
165
166 if (!booleanMethod(true, true, false, true, false, true, false, true, false, true)) {
167 throw new AssertionError();
168 }
169 }
170
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700171 private static native char charMethod(char c1, char c2, char c3, char c4, char c5, char c6, char c7,
Andreas Gamped1104322014-05-01 14:38:56 -0700172 char c8, char c9, char c10);
173
174 private static void testCharMethod() {
175 char returns[] = { (char)0, (char)1, (char)2, (char)127, (char)255, (char)256, (char)15000,
176 (char)34000 };
177 for (int i = 0; i < returns.length; i++) {
178 char result = charMethod((char)i, 'a', 'b', 'c', '0', '1', '2', (char)1234, (char)2345,
179 (char)3456);
180 if (returns[i] != result) {
181 System.out.println("Run " + i + " with " + (int)returns[i] + " vs " + (int)result);
182 throw new AssertionError();
183 }
184 }
185 }
Narayan Kamath1268b742014-07-11 19:15:11 +0100186
187 // http://b/16531674
188 private static void testIsAssignableFromOnPrimitiveTypes() {
189 if (!nativeIsAssignableFrom(int.class, Integer.TYPE)) {
190 System.out.println("IsAssignableFrom(int.class, Integer.TYPE) returned false, expected true");
191 throw new AssertionError();
192 }
193
194 if (!nativeIsAssignableFrom(Integer.TYPE, int.class)) {
195 System.out.println("IsAssignableFrom(Integer.TYPE, int.class) returned false, expected true");
196 throw new AssertionError();
197 }
198 }
199
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700200 private static native boolean nativeIsAssignableFrom(Class<?> from, Class<?> to);
Andreas Gampe718ac652014-08-11 18:51:53 -0700201
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700202 private static void testShallowGetCallingClassLoader() {
Andreas Gampe718ac652014-08-11 18:51:53 -0700203 nativeTestShallowGetCallingClassLoader();
204 }
205
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700206 private native static void nativeTestShallowGetCallingClassLoader();
Andreas Gampe0d334ce2014-08-13 23:05:38 -0700207
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700208 private static void testShallowGetStackClass2() {
Andreas Gampe0d334ce2014-08-13 23:05:38 -0700209 nativeTestShallowGetStackClass2();
210 }
211
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700212 private static native void nativeTestShallowGetStackClass2();
213
214 private static native void testCallNonvirtual();
Jeff Hao848f70a2014-01-15 13:49:50 -0800215
216 private static native void testNewStringObject();
Mathieu Chartier72156e22015-07-10 18:26:41 -0700217
218 private interface SimpleInterface {
219 void a();
220 }
221
222 private static class DummyInvocationHandler implements InvocationHandler {
223 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
224 return null;
225 }
226 }
227
228 private static void testProxyGetMethodID() {
229 InvocationHandler handler = new DummyInvocationHandler();
230 SimpleInterface proxy =
231 (SimpleInterface) Proxy.newProxyInstance(SimpleInterface.class.getClassLoader(),
Andreas Gampe166aaee2016-07-18 08:27:23 -0700232 new Class<?>[] {SimpleInterface.class}, handler);
Mathieu Chartier72156e22015-07-10 18:26:41 -0700233 if (testGetMethodID(SimpleInterface.class) == 0) {
234 throw new AssertionError();
235 }
236 if (testGetMethodID(proxy.getClass()) == 0) {
237 throw new AssertionError();
238 }
239 }
240
241 private static native long testGetMethodID(Class<?> c);
Hiroshi Yamauchi20a0be02016-02-19 15:44:06 -0800242
243 // Exercise GC and JNI critical sections in parallel.
244 private static void testJniCriticalSectionAndGc() {
245 Thread runGcThread = new Thread(new Runnable() {
246 @Override
247 public void run() {
248 for (int i = 0; i < 10; ++i) {
249 Runtime.getRuntime().gc();
250 }
251 }
252 });
253 Thread jniCriticalThread = new Thread(new Runnable() {
254 @Override
255 public void run() {
256 final int arraySize = 32;
257 byte[] array0 = new byte[arraySize];
258 byte[] array1 = new byte[arraySize];
259 enterJniCriticalSection(arraySize, array0, array1);
260 }
261 });
262 jniCriticalThread.start();
263 runGcThread.start();
264 try {
265 jniCriticalThread.join();
266 runGcThread.join();
267 } catch (InterruptedException ignored) {}
268 }
269
270 private static native void enterJniCriticalSection(int arraySize, byte[] array0, byte[] array);
Alex Lighte9d2ca22016-02-25 16:13:54 -0800271
272 private static native void testInvokeLambdaMethod(LambdaInterface iface);
273
274 private static native void testInvokeLambdaDefaultMethod(LambdaInterface iface);
Igor Murashkin9d4b6da2016-07-29 09:51:58 -0700275
276 // Test invoking @FastNative methods works correctly.
277
278 // Return sum of a+b+c.
279 @FastNative
280 static native int intFastNativeMethod(int a, int b, int c);
281
282 private static void testFastNativeMethods() {
283 int returns[] = { 0, 3, 6, 9, 12 };
284 for (int i = 0; i < returns.length; i++) {
285 int result = intFastNativeMethod(i, i, i);
286 if (returns[i] != result) {
287 System.out.println("FastNative Int Run " + i + " with " + returns[i] + " vs " + result);
288 throw new AssertionError();
289 }
290 }
291 }
292
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700293 // Smoke test for @CriticalNative
294 // TODO: Way more thorough tests since it involved quite a bit of changes.
Igor Murashkin9d4b6da2016-07-29 09:51:58 -0700295
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700296 // Return sum of a+b+c.
297 @CriticalNative
298 static native int intCriticalNativeMethod(int a, int b, int c);
299
300 private static void testCriticalNativeMethods() {
301 int returns[] = { 3, 6, 9, 12, 15 };
302 for (int i = 0; i < returns.length; i++) {
303 int result = intCriticalNativeMethod(i, i+1, i+2);
304 if (returns[i] != result) {
305 System.out.println("CriticalNative Int Run " + i + " with " + returns[i] + " vs " + result);
306 throw new AssertionError();
307 }
308 }
309 }
Alex Lighte9d2ca22016-02-25 16:13:54 -0800310}
311
312@FunctionalInterface
313interface LambdaInterface {
314 public void sayHi();
315 public default void sayHiTwice() {
316 sayHi();
317 sayHi();
318 }
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700319}
320
321class JniCallNonvirtualTest {
322 public boolean nonstaticMethodSuperCalled = false;
323 public boolean nonstaticMethodSubCalled = false;
324
325 private static native void testCallNonvirtual();
326
327 public JniCallNonvirtualTest() {
328 System.out.println("Super.<init>");
329 }
330
331 public static void staticMethod() {
332 System.out.println("Super.staticMethod");
333 }
334
335 public void nonstaticMethod() {
336 System.out.println("Super.nonstaticMethod");
337 nonstaticMethodSuperCalled = true;
338 }
339}
340
341class JniCallNonvirtualTestSubclass extends JniCallNonvirtualTest {
342
343 public JniCallNonvirtualTestSubclass() {
344 System.out.println("Subclass.<init>");
345 }
346
347 public static void staticMethod() {
348 System.out.println("Subclass.staticMethod");
349 }
350
351 public void nonstaticMethod() {
352 System.out.println("Subclass.nonstaticMethod");
353 nonstaticMethodSubCalled = true;
354 }
Brian Carlstromce888532013-10-10 00:32:58 -0700355}