Brian Carlstrom | ce88853 | 2013-10-10 00:32:58 -0700 | [diff] [blame] | 1 | /* |
| 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 Chartier | 72156e2 | 2015-07-10 18:26:41 -0700 | [diff] [blame] | 17 | import java.lang.reflect.InvocationHandler; |
Jeff Hao | 201803f | 2013-11-20 18:11:39 -0800 | [diff] [blame] | 18 | import java.lang.reflect.Method; |
Mathieu Chartier | 72156e2 | 2015-07-10 18:26:41 -0700 | [diff] [blame] | 19 | import java.lang.reflect.Proxy; |
Jeff Hao | 201803f | 2013-11-20 18:11:39 -0800 | [diff] [blame] | 20 | |
Igor Murashkin | 9d4b6da | 2016-07-29 09:51:58 -0700 | [diff] [blame] | 21 | import dalvik.annotation.optimization.FastNative; |
| 22 | |
Andreas Gampe | 1c83cbc | 2014-07-22 18:52:29 -0700 | [diff] [blame] | 23 | public class Main { |
Brian Carlstrom | ce88853 | 2013-10-10 00:32:58 -0700 | [diff] [blame] | 24 | public static void main(String[] args) { |
Mathieu Chartier | 031768a | 2015-08-27 10:25:02 -0700 | [diff] [blame] | 25 | System.loadLibrary(args[0]); |
Brian Carlstrom | ce88853 | 2013-10-10 00:32:58 -0700 | [diff] [blame] | 26 | testFindClassOnAttachedNativeThread(); |
Jeff Hao | 62509b6 | 2013-12-10 17:44:56 -0800 | [diff] [blame] | 27 | testFindFieldOnAttachedNativeThread(); |
Vladimir Marko | 3bd7a6c | 2014-06-12 15:22:31 +0100 | [diff] [blame] | 28 | testReflectFieldGetFromAttachedNativeThreadNative(); |
Brian Carlstrom | 67fe2b4 | 2013-10-15 18:51:42 -0700 | [diff] [blame] | 29 | testCallStaticVoidMethodOnSubClass(); |
Jeff Hao | 201803f | 2013-11-20 18:11:39 -0800 | [diff] [blame] | 30 | testGetMirandaMethod(); |
Narayan Kamath | ef809d0 | 2013-12-19 17:52:47 +0000 | [diff] [blame] | 31 | testZeroLengthByteBuffers(); |
Andreas Gampe | d110432 | 2014-05-01 14:38:56 -0700 | [diff] [blame] | 32 | testByteMethod(); |
| 33 | testShortMethod(); |
| 34 | testBooleanMethod(); |
| 35 | testCharMethod(); |
Narayan Kamath | 1268b74 | 2014-07-11 19:15:11 +0100 | [diff] [blame] | 36 | testIsAssignableFromOnPrimitiveTypes(); |
Andreas Gampe | 718ac65 | 2014-08-11 18:51:53 -0700 | [diff] [blame] | 37 | testShallowGetCallingClassLoader(); |
Andreas Gampe | 0d334ce | 2014-08-13 23:05:38 -0700 | [diff] [blame] | 38 | testShallowGetStackClass2(); |
Brian Carlstrom | 58e5e5d | 2014-09-07 23:52:02 -0700 | [diff] [blame] | 39 | testCallNonvirtual(); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 40 | testNewStringObject(); |
Mathieu Chartier | ff6d8cf | 2015-06-02 13:40:12 -0700 | [diff] [blame] | 41 | testRemoveLocalObject(); |
Mathieu Chartier | 72156e2 | 2015-07-10 18:26:41 -0700 | [diff] [blame] | 42 | testProxyGetMethodID(); |
Hiroshi Yamauchi | 20a0be0 | 2016-02-19 15:44:06 -0800 | [diff] [blame] | 43 | testJniCriticalSectionAndGc(); |
Alex Light | 3612149 | 2016-02-22 13:43:29 -0800 | [diff] [blame] | 44 | testCallDefaultMethods(); |
Alex Light | e9d2ca2 | 2016-02-25 16:13:54 -0800 | [diff] [blame] | 45 | String lambda = "λ"; |
| 46 | testInvokeLambdaMethod(() -> { System.out.println("hi-lambda: " + lambda); }); |
| 47 | String def = "δ"; |
| 48 | testInvokeLambdaDefaultMethod(() -> { System.out.println("hi-default " + def + lambda); }); |
Igor Murashkin | 9d4b6da | 2016-07-29 09:51:58 -0700 | [diff] [blame] | 49 | |
| 50 | registerNativesJniTest(); |
| 51 | testFastNativeMethods(); |
Brian Carlstrom | ce88853 | 2013-10-10 00:32:58 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Igor Murashkin | 9d4b6da | 2016-07-29 09:51:58 -0700 | [diff] [blame] | 54 | private static native boolean registerNativesJniTest(); |
| 55 | |
Alex Light | 3612149 | 2016-02-22 13:43:29 -0800 | [diff] [blame] | 56 | private static native void testCallDefaultMethods(); |
| 57 | |
Brian Carlstrom | ce88853 | 2013-10-10 00:32:58 -0700 | [diff] [blame] | 58 | private static native void testFindClassOnAttachedNativeThread(); |
Brian Carlstrom | 67fe2b4 | 2013-10-15 18:51:42 -0700 | [diff] [blame] | 59 | |
Jeff Hao | 62509b6 | 2013-12-10 17:44:56 -0800 | [diff] [blame] | 60 | private static boolean testFindFieldOnAttachedNativeThreadField; |
| 61 | |
Vladimir Marko | 3bd7a6c | 2014-06-12 15:22:31 +0100 | [diff] [blame] | 62 | private static native void testReflectFieldGetFromAttachedNativeThreadNative(); |
| 63 | |
| 64 | public static boolean testReflectFieldGetFromAttachedNativeThreadField; |
| 65 | |
Jeff Hao | 62509b6 | 2013-12-10 17:44:56 -0800 | [diff] [blame] | 66 | private static void testFindFieldOnAttachedNativeThread() { |
| 67 | testFindFieldOnAttachedNativeThreadNative(); |
| 68 | if (!testFindFieldOnAttachedNativeThreadField) { |
| 69 | throw new AssertionError(); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | private static native void testFindFieldOnAttachedNativeThreadNative(); |
| 74 | |
Brian Carlstrom | 67fe2b4 | 2013-10-15 18:51:42 -0700 | [diff] [blame] | 75 | private static void testCallStaticVoidMethodOnSubClass() { |
| 76 | testCallStaticVoidMethodOnSubClassNative(); |
| 77 | if (!testCallStaticVoidMethodOnSubClass_SuperClass.executed) { |
| 78 | throw new AssertionError(); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | private static native void testCallStaticVoidMethodOnSubClassNative(); |
| 83 | |
| 84 | private static class testCallStaticVoidMethodOnSubClass_SuperClass { |
| 85 | private static boolean executed = false; |
| 86 | private static void execute() { |
| 87 | executed = true; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | private static class testCallStaticVoidMethodOnSubClass_SubClass |
| 92 | extends testCallStaticVoidMethodOnSubClass_SuperClass { |
| 93 | } |
Jeff Hao | 201803f | 2013-11-20 18:11:39 -0800 | [diff] [blame] | 94 | |
| 95 | private static native Method testGetMirandaMethodNative(); |
| 96 | |
| 97 | private static void testGetMirandaMethod() { |
| 98 | Method m = testGetMirandaMethodNative(); |
| 99 | if (m.getDeclaringClass() != testGetMirandaMethod_MirandaInterface.class) { |
| 100 | throw new AssertionError(); |
| 101 | } |
| 102 | } |
| 103 | |
Narayan Kamath | ef809d0 | 2013-12-19 17:52:47 +0000 | [diff] [blame] | 104 | private static native void testZeroLengthByteBuffers(); |
| 105 | |
Jeff Hao | 201803f | 2013-11-20 18:11:39 -0800 | [diff] [blame] | 106 | private static abstract class testGetMirandaMethod_MirandaAbstract implements testGetMirandaMethod_MirandaInterface { |
| 107 | public boolean inAbstract() { |
| 108 | return true; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | private static interface testGetMirandaMethod_MirandaInterface { |
| 113 | public boolean inInterface(); |
| 114 | } |
Andreas Gampe | d110432 | 2014-05-01 14:38:56 -0700 | [diff] [blame] | 115 | |
| 116 | // Test sign-extension for values < 32b |
| 117 | |
Brian Carlstrom | 58e5e5d | 2014-09-07 23:52:02 -0700 | [diff] [blame] | 118 | static native byte byteMethod(byte b1, byte b2, byte b3, byte b4, byte b5, byte b6, byte b7, |
Andreas Gampe | d110432 | 2014-05-01 14:38:56 -0700 | [diff] [blame] | 119 | byte b8, byte b9, byte b10); |
| 120 | |
| 121 | private static void testByteMethod() { |
| 122 | byte returns[] = { 0, 1, 2, 127, -1, -2, -128 }; |
| 123 | for (int i = 0; i < returns.length; i++) { |
| 124 | byte result = byteMethod((byte)i, (byte)2, (byte)(-3), (byte)4, (byte)(-5), (byte)6, |
| 125 | (byte)(-7), (byte)8, (byte)(-9), (byte)10); |
| 126 | if (returns[i] != result) { |
| 127 | System.out.println("Run " + i + " with " + returns[i] + " vs " + result); |
| 128 | throw new AssertionError(); |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
Mathieu Chartier | ff6d8cf | 2015-06-02 13:40:12 -0700 | [diff] [blame] | 133 | private static native void removeLocalObject(Object o); |
| 134 | |
| 135 | private static void testRemoveLocalObject() { |
| 136 | removeLocalObject(new Object()); |
| 137 | } |
Alex Light | 3612149 | 2016-02-22 13:43:29 -0800 | [diff] [blame] | 138 | |
Brian Carlstrom | 58e5e5d | 2014-09-07 23:52:02 -0700 | [diff] [blame] | 139 | private static native short shortMethod(short s1, short s2, short s3, short s4, short s5, short s6, short s7, |
Andreas Gampe | d110432 | 2014-05-01 14:38:56 -0700 | [diff] [blame] | 140 | short s8, short s9, short s10); |
| 141 | |
| 142 | private static void testShortMethod() { |
| 143 | short returns[] = { 0, 1, 2, 127, 32767, -1, -2, -128, -32768 }; |
| 144 | for (int i = 0; i < returns.length; i++) { |
| 145 | short result = shortMethod((short)i, (short)2, (short)(-3), (short)4, (short)(-5), (short)6, |
| 146 | (short)(-7), (short)8, (short)(-9), (short)10); |
| 147 | if (returns[i] != result) { |
| 148 | System.out.println("Run " + i + " with " + returns[i] + " vs " + result); |
| 149 | throw new AssertionError(); |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | // Test zero-extension for values < 32b |
| 155 | |
Brian Carlstrom | 58e5e5d | 2014-09-07 23:52:02 -0700 | [diff] [blame] | 156 | private static native boolean booleanMethod(boolean b1, boolean b2, boolean b3, boolean b4, boolean b5, boolean b6, boolean b7, |
Andreas Gampe | d110432 | 2014-05-01 14:38:56 -0700 | [diff] [blame] | 157 | boolean b8, boolean b9, boolean b10); |
| 158 | |
| 159 | private static void testBooleanMethod() { |
| 160 | if (booleanMethod(false, true, false, true, false, true, false, true, false, true)) { |
| 161 | throw new AssertionError(); |
| 162 | } |
| 163 | |
| 164 | if (!booleanMethod(true, true, false, true, false, true, false, true, false, true)) { |
| 165 | throw new AssertionError(); |
| 166 | } |
| 167 | } |
| 168 | |
Brian Carlstrom | 58e5e5d | 2014-09-07 23:52:02 -0700 | [diff] [blame] | 169 | private static native char charMethod(char c1, char c2, char c3, char c4, char c5, char c6, char c7, |
Andreas Gampe | d110432 | 2014-05-01 14:38:56 -0700 | [diff] [blame] | 170 | char c8, char c9, char c10); |
| 171 | |
| 172 | private static void testCharMethod() { |
| 173 | char returns[] = { (char)0, (char)1, (char)2, (char)127, (char)255, (char)256, (char)15000, |
| 174 | (char)34000 }; |
| 175 | for (int i = 0; i < returns.length; i++) { |
| 176 | char result = charMethod((char)i, 'a', 'b', 'c', '0', '1', '2', (char)1234, (char)2345, |
| 177 | (char)3456); |
| 178 | if (returns[i] != result) { |
| 179 | System.out.println("Run " + i + " with " + (int)returns[i] + " vs " + (int)result); |
| 180 | throw new AssertionError(); |
| 181 | } |
| 182 | } |
| 183 | } |
Narayan Kamath | 1268b74 | 2014-07-11 19:15:11 +0100 | [diff] [blame] | 184 | |
| 185 | // http://b/16531674 |
| 186 | private static void testIsAssignableFromOnPrimitiveTypes() { |
| 187 | if (!nativeIsAssignableFrom(int.class, Integer.TYPE)) { |
| 188 | System.out.println("IsAssignableFrom(int.class, Integer.TYPE) returned false, expected true"); |
| 189 | throw new AssertionError(); |
| 190 | } |
| 191 | |
| 192 | if (!nativeIsAssignableFrom(Integer.TYPE, int.class)) { |
| 193 | System.out.println("IsAssignableFrom(Integer.TYPE, int.class) returned false, expected true"); |
| 194 | throw new AssertionError(); |
| 195 | } |
| 196 | } |
| 197 | |
Brian Carlstrom | 58e5e5d | 2014-09-07 23:52:02 -0700 | [diff] [blame] | 198 | private static native boolean nativeIsAssignableFrom(Class<?> from, Class<?> to); |
Andreas Gampe | 718ac65 | 2014-08-11 18:51:53 -0700 | [diff] [blame] | 199 | |
Brian Carlstrom | 58e5e5d | 2014-09-07 23:52:02 -0700 | [diff] [blame] | 200 | private static void testShallowGetCallingClassLoader() { |
Andreas Gampe | 718ac65 | 2014-08-11 18:51:53 -0700 | [diff] [blame] | 201 | nativeTestShallowGetCallingClassLoader(); |
| 202 | } |
| 203 | |
Brian Carlstrom | 58e5e5d | 2014-09-07 23:52:02 -0700 | [diff] [blame] | 204 | private native static void nativeTestShallowGetCallingClassLoader(); |
Andreas Gampe | 0d334ce | 2014-08-13 23:05:38 -0700 | [diff] [blame] | 205 | |
Brian Carlstrom | 58e5e5d | 2014-09-07 23:52:02 -0700 | [diff] [blame] | 206 | private static void testShallowGetStackClass2() { |
Andreas Gampe | 0d334ce | 2014-08-13 23:05:38 -0700 | [diff] [blame] | 207 | nativeTestShallowGetStackClass2(); |
| 208 | } |
| 209 | |
Brian Carlstrom | 58e5e5d | 2014-09-07 23:52:02 -0700 | [diff] [blame] | 210 | private static native void nativeTestShallowGetStackClass2(); |
| 211 | |
| 212 | private static native void testCallNonvirtual(); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 213 | |
| 214 | private static native void testNewStringObject(); |
Mathieu Chartier | 72156e2 | 2015-07-10 18:26:41 -0700 | [diff] [blame] | 215 | |
| 216 | private interface SimpleInterface { |
| 217 | void a(); |
| 218 | } |
| 219 | |
| 220 | private static class DummyInvocationHandler implements InvocationHandler { |
| 221 | public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { |
| 222 | return null; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | private static void testProxyGetMethodID() { |
| 227 | InvocationHandler handler = new DummyInvocationHandler(); |
| 228 | SimpleInterface proxy = |
| 229 | (SimpleInterface) Proxy.newProxyInstance(SimpleInterface.class.getClassLoader(), |
Andreas Gampe | 166aaee | 2016-07-18 08:27:23 -0700 | [diff] [blame] | 230 | new Class<?>[] {SimpleInterface.class}, handler); |
Mathieu Chartier | 72156e2 | 2015-07-10 18:26:41 -0700 | [diff] [blame] | 231 | if (testGetMethodID(SimpleInterface.class) == 0) { |
| 232 | throw new AssertionError(); |
| 233 | } |
| 234 | if (testGetMethodID(proxy.getClass()) == 0) { |
| 235 | throw new AssertionError(); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | private static native long testGetMethodID(Class<?> c); |
Hiroshi Yamauchi | 20a0be0 | 2016-02-19 15:44:06 -0800 | [diff] [blame] | 240 | |
| 241 | // Exercise GC and JNI critical sections in parallel. |
| 242 | private static void testJniCriticalSectionAndGc() { |
| 243 | Thread runGcThread = new Thread(new Runnable() { |
| 244 | @Override |
| 245 | public void run() { |
| 246 | for (int i = 0; i < 10; ++i) { |
| 247 | Runtime.getRuntime().gc(); |
| 248 | } |
| 249 | } |
| 250 | }); |
| 251 | Thread jniCriticalThread = new Thread(new Runnable() { |
| 252 | @Override |
| 253 | public void run() { |
| 254 | final int arraySize = 32; |
| 255 | byte[] array0 = new byte[arraySize]; |
| 256 | byte[] array1 = new byte[arraySize]; |
| 257 | enterJniCriticalSection(arraySize, array0, array1); |
| 258 | } |
| 259 | }); |
| 260 | jniCriticalThread.start(); |
| 261 | runGcThread.start(); |
| 262 | try { |
| 263 | jniCriticalThread.join(); |
| 264 | runGcThread.join(); |
| 265 | } catch (InterruptedException ignored) {} |
| 266 | } |
| 267 | |
| 268 | private static native void enterJniCriticalSection(int arraySize, byte[] array0, byte[] array); |
Alex Light | e9d2ca2 | 2016-02-25 16:13:54 -0800 | [diff] [blame] | 269 | |
| 270 | private static native void testInvokeLambdaMethod(LambdaInterface iface); |
| 271 | |
| 272 | private static native void testInvokeLambdaDefaultMethod(LambdaInterface iface); |
Igor Murashkin | 9d4b6da | 2016-07-29 09:51:58 -0700 | [diff] [blame] | 273 | |
| 274 | // Test invoking @FastNative methods works correctly. |
| 275 | |
| 276 | // Return sum of a+b+c. |
| 277 | @FastNative |
| 278 | static native int intFastNativeMethod(int a, int b, int c); |
| 279 | |
| 280 | private static void testFastNativeMethods() { |
| 281 | int returns[] = { 0, 3, 6, 9, 12 }; |
| 282 | for (int i = 0; i < returns.length; i++) { |
| 283 | int result = intFastNativeMethod(i, i, i); |
| 284 | if (returns[i] != result) { |
| 285 | System.out.println("FastNative Int Run " + i + " with " + returns[i] + " vs " + result); |
| 286 | throw new AssertionError(); |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | |
Alex Light | e9d2ca2 | 2016-02-25 16:13:54 -0800 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | @FunctionalInterface |
| 295 | interface LambdaInterface { |
| 296 | public void sayHi(); |
| 297 | public default void sayHiTwice() { |
| 298 | sayHi(); |
| 299 | sayHi(); |
| 300 | } |
Brian Carlstrom | 58e5e5d | 2014-09-07 23:52:02 -0700 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | class JniCallNonvirtualTest { |
| 304 | public boolean nonstaticMethodSuperCalled = false; |
| 305 | public boolean nonstaticMethodSubCalled = false; |
| 306 | |
| 307 | private static native void testCallNonvirtual(); |
| 308 | |
| 309 | public JniCallNonvirtualTest() { |
| 310 | System.out.println("Super.<init>"); |
| 311 | } |
| 312 | |
| 313 | public static void staticMethod() { |
| 314 | System.out.println("Super.staticMethod"); |
| 315 | } |
| 316 | |
| 317 | public void nonstaticMethod() { |
| 318 | System.out.println("Super.nonstaticMethod"); |
| 319 | nonstaticMethodSuperCalled = true; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | class JniCallNonvirtualTestSubclass extends JniCallNonvirtualTest { |
| 324 | |
| 325 | public JniCallNonvirtualTestSubclass() { |
| 326 | System.out.println("Subclass.<init>"); |
| 327 | } |
| 328 | |
| 329 | public static void staticMethod() { |
| 330 | System.out.println("Subclass.staticMethod"); |
| 331 | } |
| 332 | |
| 333 | public void nonstaticMethod() { |
| 334 | System.out.println("Subclass.nonstaticMethod"); |
| 335 | nonstaticMethodSubCalled = true; |
| 336 | } |
Brian Carlstrom | ce88853 | 2013-10-10 00:32:58 -0700 | [diff] [blame] | 337 | } |