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 | 367f3dd | 2016-09-01 17:00:24 -0700 | [diff] [blame] | 21 | import dalvik.annotation.optimization.CriticalNative; |
Igor Murashkin | 9d4b6da | 2016-07-29 09:51:58 -0700 | [diff] [blame] | 22 | import dalvik.annotation.optimization.FastNative; |
| 23 | |
Andreas Gampe | 1c83cbc | 2014-07-22 18:52:29 -0700 | [diff] [blame] | 24 | public class Main { |
Brian Carlstrom | ce88853 | 2013-10-10 00:32:58 -0700 | [diff] [blame] | 25 | public static void main(String[] args) { |
Mathieu Chartier | 031768a | 2015-08-27 10:25:02 -0700 | [diff] [blame] | 26 | System.loadLibrary(args[0]); |
Brian Carlstrom | ce88853 | 2013-10-10 00:32:58 -0700 | [diff] [blame] | 27 | testFindClassOnAttachedNativeThread(); |
Jeff Hao | 62509b6 | 2013-12-10 17:44:56 -0800 | [diff] [blame] | 28 | testFindFieldOnAttachedNativeThread(); |
Vladimir Marko | 3bd7a6c | 2014-06-12 15:22:31 +0100 | [diff] [blame] | 29 | testReflectFieldGetFromAttachedNativeThreadNative(); |
Brian Carlstrom | 67fe2b4 | 2013-10-15 18:51:42 -0700 | [diff] [blame] | 30 | testCallStaticVoidMethodOnSubClass(); |
Jeff Hao | 201803f | 2013-11-20 18:11:39 -0800 | [diff] [blame] | 31 | testGetMirandaMethod(); |
Narayan Kamath | ef809d0 | 2013-12-19 17:52:47 +0000 | [diff] [blame] | 32 | testZeroLengthByteBuffers(); |
Andreas Gampe | d110432 | 2014-05-01 14:38:56 -0700 | [diff] [blame] | 33 | testByteMethod(); |
| 34 | testShortMethod(); |
| 35 | testBooleanMethod(); |
| 36 | testCharMethod(); |
Narayan Kamath | 1268b74 | 2014-07-11 19:15:11 +0100 | [diff] [blame] | 37 | testIsAssignableFromOnPrimitiveTypes(); |
Andreas Gampe | 718ac65 | 2014-08-11 18:51:53 -0700 | [diff] [blame] | 38 | testShallowGetCallingClassLoader(); |
Andreas Gampe | 0d334ce | 2014-08-13 23:05:38 -0700 | [diff] [blame] | 39 | testShallowGetStackClass2(); |
Brian Carlstrom | 58e5e5d | 2014-09-07 23:52:02 -0700 | [diff] [blame] | 40 | testCallNonvirtual(); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 41 | testNewStringObject(); |
Mathieu Chartier | ff6d8cf | 2015-06-02 13:40:12 -0700 | [diff] [blame] | 42 | testRemoveLocalObject(); |
Mathieu Chartier | 72156e2 | 2015-07-10 18:26:41 -0700 | [diff] [blame] | 43 | testProxyGetMethodID(); |
Hiroshi Yamauchi | 20a0be0 | 2016-02-19 15:44:06 -0800 | [diff] [blame] | 44 | testJniCriticalSectionAndGc(); |
Alex Light | 3612149 | 2016-02-22 13:43:29 -0800 | [diff] [blame] | 45 | testCallDefaultMethods(); |
Alex Light | e9d2ca2 | 2016-02-25 16:13:54 -0800 | [diff] [blame] | 46 | String lambda = "λ"; |
| 47 | testInvokeLambdaMethod(() -> { System.out.println("hi-lambda: " + lambda); }); |
| 48 | String def = "δ"; |
| 49 | testInvokeLambdaDefaultMethod(() -> { System.out.println("hi-default " + def + lambda); }); |
Igor Murashkin | 9d4b6da | 2016-07-29 09:51:58 -0700 | [diff] [blame] | 50 | |
| 51 | registerNativesJniTest(); |
| 52 | testFastNativeMethods(); |
Igor Murashkin | 367f3dd | 2016-09-01 17:00:24 -0700 | [diff] [blame] | 53 | testCriticalNativeMethods(); |
Brian Carlstrom | ce88853 | 2013-10-10 00:32:58 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Igor Murashkin | 9d4b6da | 2016-07-29 09:51:58 -0700 | [diff] [blame] | 56 | private static native boolean registerNativesJniTest(); |
| 57 | |
Alex Light | 3612149 | 2016-02-22 13:43:29 -0800 | [diff] [blame] | 58 | private static native void testCallDefaultMethods(); |
| 59 | |
Brian Carlstrom | ce88853 | 2013-10-10 00:32:58 -0700 | [diff] [blame] | 60 | private static native void testFindClassOnAttachedNativeThread(); |
Brian Carlstrom | 67fe2b4 | 2013-10-15 18:51:42 -0700 | [diff] [blame] | 61 | |
Jeff Hao | 62509b6 | 2013-12-10 17:44:56 -0800 | [diff] [blame] | 62 | private static boolean testFindFieldOnAttachedNativeThreadField; |
| 63 | |
Vladimir Marko | 3bd7a6c | 2014-06-12 15:22:31 +0100 | [diff] [blame] | 64 | private static native void testReflectFieldGetFromAttachedNativeThreadNative(); |
| 65 | |
| 66 | public static boolean testReflectFieldGetFromAttachedNativeThreadField; |
| 67 | |
Jeff Hao | 62509b6 | 2013-12-10 17:44:56 -0800 | [diff] [blame] | 68 | private static void testFindFieldOnAttachedNativeThread() { |
| 69 | testFindFieldOnAttachedNativeThreadNative(); |
| 70 | if (!testFindFieldOnAttachedNativeThreadField) { |
| 71 | throw new AssertionError(); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | private static native void testFindFieldOnAttachedNativeThreadNative(); |
| 76 | |
Brian Carlstrom | 67fe2b4 | 2013-10-15 18:51:42 -0700 | [diff] [blame] | 77 | 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 Hao | 201803f | 2013-11-20 18:11:39 -0800 | [diff] [blame] | 96 | |
| 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 Kamath | ef809d0 | 2013-12-19 17:52:47 +0000 | [diff] [blame] | 106 | private static native void testZeroLengthByteBuffers(); |
| 107 | |
Jeff Hao | 201803f | 2013-11-20 18:11:39 -0800 | [diff] [blame] | 108 | 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 Gampe | d110432 | 2014-05-01 14:38:56 -0700 | [diff] [blame] | 117 | |
| 118 | // Test sign-extension for values < 32b |
| 119 | |
Brian Carlstrom | 58e5e5d | 2014-09-07 23:52:02 -0700 | [diff] [blame] | 120 | 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] | 121 | 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 Chartier | ff6d8cf | 2015-06-02 13:40:12 -0700 | [diff] [blame] | 135 | private static native void removeLocalObject(Object o); |
| 136 | |
| 137 | private static void testRemoveLocalObject() { |
| 138 | removeLocalObject(new Object()); |
| 139 | } |
Alex Light | 3612149 | 2016-02-22 13:43:29 -0800 | [diff] [blame] | 140 | |
Brian Carlstrom | 58e5e5d | 2014-09-07 23:52:02 -0700 | [diff] [blame] | 141 | 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] | 142 | 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 Carlstrom | 58e5e5d | 2014-09-07 23:52:02 -0700 | [diff] [blame] | 158 | 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] | 159 | 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 Carlstrom | 58e5e5d | 2014-09-07 23:52:02 -0700 | [diff] [blame] | 171 | 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] | 172 | 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 Kamath | 1268b74 | 2014-07-11 19:15:11 +0100 | [diff] [blame] | 186 | |
| 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 Carlstrom | 58e5e5d | 2014-09-07 23:52:02 -0700 | [diff] [blame] | 200 | private static native boolean nativeIsAssignableFrom(Class<?> from, Class<?> to); |
Andreas Gampe | 718ac65 | 2014-08-11 18:51:53 -0700 | [diff] [blame] | 201 | |
Brian Carlstrom | 58e5e5d | 2014-09-07 23:52:02 -0700 | [diff] [blame] | 202 | private static void testShallowGetCallingClassLoader() { |
Andreas Gampe | 718ac65 | 2014-08-11 18:51:53 -0700 | [diff] [blame] | 203 | nativeTestShallowGetCallingClassLoader(); |
| 204 | } |
| 205 | |
Brian Carlstrom | 58e5e5d | 2014-09-07 23:52:02 -0700 | [diff] [blame] | 206 | private native static void nativeTestShallowGetCallingClassLoader(); |
Andreas Gampe | 0d334ce | 2014-08-13 23:05:38 -0700 | [diff] [blame] | 207 | |
Brian Carlstrom | 58e5e5d | 2014-09-07 23:52:02 -0700 | [diff] [blame] | 208 | private static void testShallowGetStackClass2() { |
Andreas Gampe | 0d334ce | 2014-08-13 23:05:38 -0700 | [diff] [blame] | 209 | nativeTestShallowGetStackClass2(); |
| 210 | } |
| 211 | |
Brian Carlstrom | 58e5e5d | 2014-09-07 23:52:02 -0700 | [diff] [blame] | 212 | private static native void nativeTestShallowGetStackClass2(); |
| 213 | |
| 214 | private static native void testCallNonvirtual(); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 215 | |
| 216 | private static native void testNewStringObject(); |
Mathieu Chartier | 72156e2 | 2015-07-10 18:26:41 -0700 | [diff] [blame] | 217 | |
| 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 Gampe | 166aaee | 2016-07-18 08:27:23 -0700 | [diff] [blame] | 232 | new Class<?>[] {SimpleInterface.class}, handler); |
Mathieu Chartier | 72156e2 | 2015-07-10 18:26:41 -0700 | [diff] [blame] | 233 | 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 Yamauchi | 20a0be0 | 2016-02-19 15:44:06 -0800 | [diff] [blame] | 242 | |
| 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 Light | e9d2ca2 | 2016-02-25 16:13:54 -0800 | [diff] [blame] | 271 | |
| 272 | private static native void testInvokeLambdaMethod(LambdaInterface iface); |
| 273 | |
| 274 | private static native void testInvokeLambdaDefaultMethod(LambdaInterface iface); |
Igor Murashkin | 9d4b6da | 2016-07-29 09:51:58 -0700 | [diff] [blame] | 275 | |
| 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 Murashkin | 367f3dd | 2016-09-01 17:00:24 -0700 | [diff] [blame] | 293 | // Smoke test for @CriticalNative |
| 294 | // TODO: Way more thorough tests since it involved quite a bit of changes. |
Igor Murashkin | 9d4b6da | 2016-07-29 09:51:58 -0700 | [diff] [blame] | 295 | |
Igor Murashkin | 367f3dd | 2016-09-01 17:00:24 -0700 | [diff] [blame] | 296 | // 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 Light | e9d2ca2 | 2016-02-25 16:13:54 -0800 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | @FunctionalInterface |
| 313 | interface LambdaInterface { |
| 314 | public void sayHi(); |
| 315 | public default void sayHiTwice() { |
| 316 | sayHi(); |
| 317 | sayHi(); |
| 318 | } |
Brian Carlstrom | 58e5e5d | 2014-09-07 23:52:02 -0700 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | class 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 | |
| 341 | class 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 Carlstrom | ce88853 | 2013-10-10 00:32:58 -0700 | [diff] [blame] | 355 | } |