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