xref: /aosp_15_r20/art/test/115-native-bridge/src/NativeBridgeMain.java (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2014 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker import java.lang.reflect.Method;
18*795d594fSAndroid Build Coastguard Worker import java.lang.System;
19*795d594fSAndroid Build Coastguard Worker import java.lang.Exception;
20*795d594fSAndroid Build Coastguard Worker 
21*795d594fSAndroid Build Coastguard Worker // This is named Main as it is a copy of JniTest, so that we can re-use the native implementations
22*795d594fSAndroid Build Coastguard Worker // from libarttest.
23*795d594fSAndroid Build Coastguard Worker class Main {
main(String[] args)24*795d594fSAndroid Build Coastguard Worker     public static void main(String[] args) {
25*795d594fSAndroid Build Coastguard Worker         testFindClassOnAttachedNativeThread();
26*795d594fSAndroid Build Coastguard Worker         testFindFieldOnAttachedNativeThread();
27*795d594fSAndroid Build Coastguard Worker         testCallStaticVoidMethodOnSubClass();
28*795d594fSAndroid Build Coastguard Worker         testGetMirandaMethod();
29*795d594fSAndroid Build Coastguard Worker         testZeroLengthByteBuffers();
30*795d594fSAndroid Build Coastguard Worker         testByteMethod();
31*795d594fSAndroid Build Coastguard Worker         testShortMethod();
32*795d594fSAndroid Build Coastguard Worker         testBooleanMethod();
33*795d594fSAndroid Build Coastguard Worker         testCharMethod();
34*795d594fSAndroid Build Coastguard Worker         testEnvironment();
35*795d594fSAndroid Build Coastguard Worker         testNewStringObject();
36*795d594fSAndroid Build Coastguard Worker         testSignalHandler();
37*795d594fSAndroid Build Coastguard Worker         testGetErrorByLoadInvalidLibrary();
38*795d594fSAndroid Build Coastguard Worker         testSignalHandlerNotReturn();
39*795d594fSAndroid Build Coastguard Worker     }
40*795d594fSAndroid Build Coastguard Worker 
testFindClassOnAttachedNativeThread()41*795d594fSAndroid Build Coastguard Worker     public static native void testFindClassOnAttachedNativeThread();
42*795d594fSAndroid Build Coastguard Worker 
43*795d594fSAndroid Build Coastguard Worker     public static boolean testFindFieldOnAttachedNativeThreadField;
44*795d594fSAndroid Build Coastguard Worker 
testFindFieldOnAttachedNativeThread()45*795d594fSAndroid Build Coastguard Worker     public static void testFindFieldOnAttachedNativeThread() {
46*795d594fSAndroid Build Coastguard Worker       testFindFieldOnAttachedNativeThreadNative();
47*795d594fSAndroid Build Coastguard Worker       if (!testFindFieldOnAttachedNativeThreadField) {
48*795d594fSAndroid Build Coastguard Worker             throw new AssertionError();
49*795d594fSAndroid Build Coastguard Worker         }
50*795d594fSAndroid Build Coastguard Worker     }
51*795d594fSAndroid Build Coastguard Worker 
testFindFieldOnAttachedNativeThreadNative()52*795d594fSAndroid Build Coastguard Worker     private static native void testFindFieldOnAttachedNativeThreadNative();
53*795d594fSAndroid Build Coastguard Worker 
testCallStaticVoidMethodOnSubClass()54*795d594fSAndroid Build Coastguard Worker     private static void testCallStaticVoidMethodOnSubClass() {
55*795d594fSAndroid Build Coastguard Worker         testCallStaticVoidMethodOnSubClassNative();
56*795d594fSAndroid Build Coastguard Worker         if (!testCallStaticVoidMethodOnSubClass_SuperClass.executed) {
57*795d594fSAndroid Build Coastguard Worker             throw new AssertionError();
58*795d594fSAndroid Build Coastguard Worker         }
59*795d594fSAndroid Build Coastguard Worker     }
60*795d594fSAndroid Build Coastguard Worker 
testCallStaticVoidMethodOnSubClassNative()61*795d594fSAndroid Build Coastguard Worker     private static native void testCallStaticVoidMethodOnSubClassNative();
62*795d594fSAndroid Build Coastguard Worker 
63*795d594fSAndroid Build Coastguard Worker     private static class testCallStaticVoidMethodOnSubClass_SuperClass {
64*795d594fSAndroid Build Coastguard Worker         private static boolean executed = false;
execute()65*795d594fSAndroid Build Coastguard Worker         private static void execute() {
66*795d594fSAndroid Build Coastguard Worker             executed = true;
67*795d594fSAndroid Build Coastguard Worker         }
68*795d594fSAndroid Build Coastguard Worker     }
69*795d594fSAndroid Build Coastguard Worker 
70*795d594fSAndroid Build Coastguard Worker     private static class testCallStaticVoidMethodOnSubClass_SubClass
71*795d594fSAndroid Build Coastguard Worker         extends testCallStaticVoidMethodOnSubClass_SuperClass {
72*795d594fSAndroid Build Coastguard Worker     }
73*795d594fSAndroid Build Coastguard Worker 
testGetMirandaMethodNative()74*795d594fSAndroid Build Coastguard Worker     private static native Method testGetMirandaMethodNative();
75*795d594fSAndroid Build Coastguard Worker 
testGetMirandaMethod()76*795d594fSAndroid Build Coastguard Worker     private static void testGetMirandaMethod() {
77*795d594fSAndroid Build Coastguard Worker         Method m = testGetMirandaMethodNative();
78*795d594fSAndroid Build Coastguard Worker         if (m.getDeclaringClass() != testGetMirandaMethod_MirandaInterface.class) {
79*795d594fSAndroid Build Coastguard Worker             throw new AssertionError();
80*795d594fSAndroid Build Coastguard Worker         }
81*795d594fSAndroid Build Coastguard Worker     }
82*795d594fSAndroid Build Coastguard Worker 
testZeroLengthByteBuffers()83*795d594fSAndroid Build Coastguard Worker     private static native void testZeroLengthByteBuffers();
84*795d594fSAndroid Build Coastguard Worker 
85*795d594fSAndroid Build Coastguard Worker     private static abstract class testGetMirandaMethod_MirandaAbstract implements testGetMirandaMethod_MirandaInterface {
inAbstract()86*795d594fSAndroid Build Coastguard Worker         public boolean inAbstract() {
87*795d594fSAndroid Build Coastguard Worker             return true;
88*795d594fSAndroid Build Coastguard Worker         }
89*795d594fSAndroid Build Coastguard Worker     }
90*795d594fSAndroid Build Coastguard Worker 
91*795d594fSAndroid Build Coastguard Worker     private static interface testGetMirandaMethod_MirandaInterface {
inInterface()92*795d594fSAndroid Build Coastguard Worker         public boolean inInterface();
93*795d594fSAndroid Build Coastguard Worker     }
94*795d594fSAndroid Build Coastguard Worker 
95*795d594fSAndroid Build Coastguard Worker     // Test sign-extension for values < 32b
96*795d594fSAndroid Build Coastguard Worker 
byteMethod(byte b1, byte b2, byte b3, byte b4, byte b5, byte b6, byte b7, byte b8, byte b9, byte b10)97*795d594fSAndroid Build Coastguard Worker     native static byte byteMethod(byte b1, byte b2, byte b3, byte b4, byte b5, byte b6, byte b7,
98*795d594fSAndroid Build Coastguard Worker         byte b8, byte b9, byte b10);
99*795d594fSAndroid Build Coastguard Worker 
testByteMethod()100*795d594fSAndroid Build Coastguard Worker     public static void testByteMethod() {
101*795d594fSAndroid Build Coastguard Worker       byte returns[] = { 0, 1, 2, 127, -1, -2, -128 };
102*795d594fSAndroid Build Coastguard Worker       for (int i = 0; i < returns.length; i++) {
103*795d594fSAndroid Build Coastguard Worker         byte result = byteMethod((byte)i, (byte)2, (byte)(-3), (byte)4, (byte)(-5), (byte)6,
104*795d594fSAndroid Build Coastguard Worker             (byte)(-7), (byte)8, (byte)(-9), (byte)10);
105*795d594fSAndroid Build Coastguard Worker         if (returns[i] != result) {
106*795d594fSAndroid Build Coastguard Worker           System.out.println("Run " + i + " with " + returns[i] + " vs " + result);
107*795d594fSAndroid Build Coastguard Worker           throw new AssertionError();
108*795d594fSAndroid Build Coastguard Worker         }
109*795d594fSAndroid Build Coastguard Worker       }
110*795d594fSAndroid Build Coastguard Worker     }
111*795d594fSAndroid Build Coastguard Worker 
shortMethod(short s1, short s2, short s3, short s4, short s5, short s6, short s7, short s8, short s9, short s10)112*795d594fSAndroid Build Coastguard Worker     native static short shortMethod(short s1, short s2, short s3, short s4, short s5, short s6, short s7,
113*795d594fSAndroid Build Coastguard Worker         short s8, short s9, short s10);
114*795d594fSAndroid Build Coastguard Worker 
testShortMethod()115*795d594fSAndroid Build Coastguard Worker     private static void testShortMethod() {
116*795d594fSAndroid Build Coastguard Worker       short returns[] = { 0, 1, 2, 127, 32767, -1, -2, -128, -32768 };
117*795d594fSAndroid Build Coastguard Worker       for (int i = 0; i < returns.length; i++) {
118*795d594fSAndroid Build Coastguard Worker         short result = shortMethod((short)i, (short)2, (short)(-3), (short)4, (short)(-5), (short)6,
119*795d594fSAndroid Build Coastguard Worker             (short)(-7), (short)8, (short)(-9), (short)10);
120*795d594fSAndroid Build Coastguard Worker         if (returns[i] != result) {
121*795d594fSAndroid Build Coastguard Worker           System.out.println("Run " + i + " with " + returns[i] + " vs " + result);
122*795d594fSAndroid Build Coastguard Worker           throw new AssertionError();
123*795d594fSAndroid Build Coastguard Worker         }
124*795d594fSAndroid Build Coastguard Worker       }
125*795d594fSAndroid Build Coastguard Worker     }
126*795d594fSAndroid Build Coastguard Worker 
127*795d594fSAndroid Build Coastguard Worker     // Test zero-extension for values < 32b
128*795d594fSAndroid Build Coastguard Worker 
booleanMethod(boolean b1, boolean b2, boolean b3, boolean b4, boolean b5, boolean b6, boolean b7, boolean b8, boolean b9, boolean b10)129*795d594fSAndroid Build Coastguard Worker     native static boolean booleanMethod(boolean b1, boolean b2, boolean b3, boolean b4, boolean b5, boolean b6, boolean b7,
130*795d594fSAndroid Build Coastguard Worker         boolean b8, boolean b9, boolean b10);
131*795d594fSAndroid Build Coastguard Worker 
testBooleanMethod()132*795d594fSAndroid Build Coastguard Worker     public static void testBooleanMethod() {
133*795d594fSAndroid Build Coastguard Worker       if (booleanMethod(false, true, false, true, false, true, false, true, false, true)) {
134*795d594fSAndroid Build Coastguard Worker         throw new AssertionError();
135*795d594fSAndroid Build Coastguard Worker       }
136*795d594fSAndroid Build Coastguard Worker 
137*795d594fSAndroid Build Coastguard Worker       if (!booleanMethod(true, true, false, true, false, true, false, true, false, true)) {
138*795d594fSAndroid Build Coastguard Worker         throw new AssertionError();
139*795d594fSAndroid Build Coastguard Worker       }
140*795d594fSAndroid Build Coastguard Worker     }
141*795d594fSAndroid Build Coastguard Worker 
charMethod(char c1, char c2, char c3, char c4, char c5, char c6, char c7, char c8, char c9, char c10)142*795d594fSAndroid Build Coastguard Worker     native static char charMethod(char c1, char c2, char c3, char c4, char c5, char c6, char c7,
143*795d594fSAndroid Build Coastguard Worker         char c8, char c9, char c10);
144*795d594fSAndroid Build Coastguard Worker 
testCharMethod()145*795d594fSAndroid Build Coastguard Worker     private static void testCharMethod() {
146*795d594fSAndroid Build Coastguard Worker       char returns[] = { (char)0, (char)1, (char)2, (char)127, (char)255, (char)256, (char)15000,
147*795d594fSAndroid Build Coastguard Worker           (char)34000 };
148*795d594fSAndroid Build Coastguard Worker       for (int i = 0; i < returns.length; i++) {
149*795d594fSAndroid Build Coastguard Worker         char result = charMethod((char)i, 'a', 'b', 'c', '0', '1', '2', (char)1234, (char)2345,
150*795d594fSAndroid Build Coastguard Worker             (char)3456);
151*795d594fSAndroid Build Coastguard Worker         if (returns[i] != result) {
152*795d594fSAndroid Build Coastguard Worker           System.out.println("Run " + i + " with " + (int)returns[i] + " vs " + (int)result);
153*795d594fSAndroid Build Coastguard Worker           throw new AssertionError();
154*795d594fSAndroid Build Coastguard Worker         }
155*795d594fSAndroid Build Coastguard Worker       }
156*795d594fSAndroid Build Coastguard Worker     }
157*795d594fSAndroid Build Coastguard Worker 
testEnvironment()158*795d594fSAndroid Build Coastguard Worker     private static void testEnvironment() {
159*795d594fSAndroid Build Coastguard Worker       String osArch = System.getProperty("os.arch");
160*795d594fSAndroid Build Coastguard Worker       if (!"os.arch".equals(osArch)) {
161*795d594fSAndroid Build Coastguard Worker         throw new AssertionError("unexpected value for os.arch: " + osArch);
162*795d594fSAndroid Build Coastguard Worker       }
163*795d594fSAndroid Build Coastguard Worker       // TODO: improve the build script to get these running as well.
164*795d594fSAndroid Build Coastguard Worker       // if (!"cpu_abi".equals(Build.CPU_ABI)) {
165*795d594fSAndroid Build Coastguard Worker       //   throw new AssertionError("unexpected value for cpu_abi");
166*795d594fSAndroid Build Coastguard Worker       // }
167*795d594fSAndroid Build Coastguard Worker       // if (!"cpu_abi2".equals(Build.CPU_ABI2)) {
168*795d594fSAndroid Build Coastguard Worker       //   throw new AssertionError("unexpected value for cpu_abi2");
169*795d594fSAndroid Build Coastguard Worker       // }
170*795d594fSAndroid Build Coastguard Worker       // String[] expectedSupportedAbis = {"supported1", "supported2", "supported3"};
171*795d594fSAndroid Build Coastguard Worker       // if (Arrays.equals(expectedSupportedAbis, Build.SUPPORTED_ABIS)) {
172*795d594fSAndroid Build Coastguard Worker       //   throw new AssertionError("unexpected value for supported_abis");
173*795d594fSAndroid Build Coastguard Worker       // }
174*795d594fSAndroid Build Coastguard Worker     }
175*795d594fSAndroid Build Coastguard Worker 
testNewStringObject()176*795d594fSAndroid Build Coastguard Worker     private static native void testNewStringObject();
177*795d594fSAndroid Build Coastguard Worker 
178*795d594fSAndroid Build Coastguard Worker     // Test v2 special signal handlers. This uses the native code from 004-SignalTest to cause
179*795d594fSAndroid Build Coastguard Worker     // a non-managed segfault.
testSignalHandler()180*795d594fSAndroid Build Coastguard Worker     private static void testSignalHandler() {
181*795d594fSAndroid Build Coastguard Worker         // This uses code from 004-SignalTest.
182*795d594fSAndroid Build Coastguard Worker         int x = testSignal();
183*795d594fSAndroid Build Coastguard Worker         if (x != 1234) {
184*795d594fSAndroid Build Coastguard Worker             throw new AssertionError();
185*795d594fSAndroid Build Coastguard Worker         }
186*795d594fSAndroid Build Coastguard Worker     }
187*795d594fSAndroid Build Coastguard Worker 
testSignal()188*795d594fSAndroid Build Coastguard Worker     private static native int testSignal();
189*795d594fSAndroid Build Coastguard Worker 
190*795d594fSAndroid Build Coastguard Worker     // Test the path from Java to getError() of NativeBridge.
191*795d594fSAndroid Build Coastguard Worker     //
192*795d594fSAndroid Build Coastguard Worker     // Load invalid library 'libinvalid.so' from Java. Library loading will fail since it's
193*795d594fSAndroid Build Coastguard Worker     // invalid (empty file). ART, NativeLoader actually, calls getError() to dump error message.
194*795d594fSAndroid Build Coastguard Worker     // After that in Java, catch UnsatisfiedLinkError exception to confirm.
testGetErrorByLoadInvalidLibrary()195*795d594fSAndroid Build Coastguard Worker     private static void testGetErrorByLoadInvalidLibrary() {
196*795d594fSAndroid Build Coastguard Worker         System.out.println("Loading invalid library 'libinvalid.so' from Java, which will fail.");
197*795d594fSAndroid Build Coastguard Worker         try {
198*795d594fSAndroid Build Coastguard Worker             System.loadLibrary("invalid");
199*795d594fSAndroid Build Coastguard Worker         } catch (java.lang.UnsatisfiedLinkError e){
200*795d594fSAndroid Build Coastguard Worker             System.out.println("Catch UnsatisfiedLinkError exception as expected.");
201*795d594fSAndroid Build Coastguard Worker         }
202*795d594fSAndroid Build Coastguard Worker     }
203*795d594fSAndroid Build Coastguard Worker 
testSignalHandlerNotReturn()204*795d594fSAndroid Build Coastguard Worker     private static native void testSignalHandlerNotReturn();
205*795d594fSAndroid Build Coastguard Worker }
206*795d594fSAndroid Build Coastguard Worker 
207*795d594fSAndroid Build Coastguard Worker public class NativeBridgeMain {
main(String[] args)208*795d594fSAndroid Build Coastguard Worker     static public void main(String[] args) throws Exception {
209*795d594fSAndroid Build Coastguard Worker         System.out.println("Ready for native bridge tests.");
210*795d594fSAndroid Build Coastguard Worker 
211*795d594fSAndroid Build Coastguard Worker         System.loadLibrary(args[0]);
212*795d594fSAndroid Build Coastguard Worker 
213*795d594fSAndroid Build Coastguard Worker         Main.main(null);
214*795d594fSAndroid Build Coastguard Worker     }
215*795d594fSAndroid Build Coastguard Worker }
216