xref: /aosp_15_r20/art/test/1973-jni-id-swap-pointer/src/Main.java (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2019 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 public class Main {
18*795d594fSAndroid Build Coastguard Worker   public static final boolean PRINT = false;
19*795d594fSAndroid Build Coastguard Worker 
20*795d594fSAndroid Build Coastguard Worker   public static class PtrCls {
doNothingPtr()21*795d594fSAndroid Build Coastguard Worker     public static void doNothingPtr() {}
22*795d594fSAndroid Build Coastguard Worker   }
23*795d594fSAndroid Build Coastguard Worker 
24*795d594fSAndroid Build Coastguard Worker   public static class IdxCls {
doNothingIdx()25*795d594fSAndroid Build Coastguard Worker     public static void doNothingIdx() {}
26*795d594fSAndroid Build Coastguard Worker   }
27*795d594fSAndroid Build Coastguard Worker 
DbgPrint(String str)28*795d594fSAndroid Build Coastguard Worker   public static void DbgPrint(String str) {
29*795d594fSAndroid Build Coastguard Worker     if (PRINT) {
30*795d594fSAndroid Build Coastguard Worker       System.out.println(str);
31*795d594fSAndroid Build Coastguard Worker     }
32*795d594fSAndroid Build Coastguard Worker   }
33*795d594fSAndroid Build Coastguard Worker 
GetId(Class<?> c, String name)34*795d594fSAndroid Build Coastguard Worker   public static long GetId(Class<?> c, String name) {
35*795d594fSAndroid Build Coastguard Worker     return GetMethodId(true, c, name, "()V");
36*795d594fSAndroid Build Coastguard Worker   }
37*795d594fSAndroid Build Coastguard Worker 
main(String[] args)38*795d594fSAndroid Build Coastguard Worker   public static void main(String[] args) {
39*795d594fSAndroid Build Coastguard Worker     System.loadLibrary(args[0]);
40*795d594fSAndroid Build Coastguard Worker     System.out.println("JNI Type is: " + GetJniType());
41*795d594fSAndroid Build Coastguard Worker     long expect_ptr_id = GetId(PtrCls.class, "doNothingPtr");
42*795d594fSAndroid Build Coastguard Worker     DbgPrint(String.format("expected_ptr_id is 0x%x", expect_ptr_id));
43*795d594fSAndroid Build Coastguard Worker     if (expect_ptr_id % 4 != 0) {
44*795d594fSAndroid Build Coastguard Worker       throw new Error("ID " + expect_ptr_id + " is not aligned!");
45*795d594fSAndroid Build Coastguard Worker     } else {
46*795d594fSAndroid Build Coastguard Worker       System.out.println("pointer ID looks like a pointer!");
47*795d594fSAndroid Build Coastguard Worker     }
48*795d594fSAndroid Build Coastguard Worker     SetToPointerIds();
49*795d594fSAndroid Build Coastguard Worker     System.out.println("JNI Type is: " + GetJniType());
50*795d594fSAndroid Build Coastguard Worker     long expect_ptr_id2 = GetId(IdxCls.class, "doNothingIdx");
51*795d594fSAndroid Build Coastguard Worker     DbgPrint(String.format("expected_ptr_id2 is 0x%x", expect_ptr_id2));
52*795d594fSAndroid Build Coastguard Worker     if (expect_ptr_id2 % 4 != 0) {
53*795d594fSAndroid Build Coastguard Worker       throw new Error("ID " + expect_ptr_id + " is not aligned!");
54*795d594fSAndroid Build Coastguard Worker     } else {
55*795d594fSAndroid Build Coastguard Worker       System.out.println("pointer2 ID looks like a pointer!");
56*795d594fSAndroid Build Coastguard Worker     }
57*795d594fSAndroid Build Coastguard Worker     long again_ptr_id = GetId(PtrCls.class, "doNothingPtr");
58*795d594fSAndroid Build Coastguard Worker     if (expect_ptr_id != again_ptr_id) {
59*795d594fSAndroid Build Coastguard Worker       throw new Error(
60*795d594fSAndroid Build Coastguard Worker           "Got different id values for same method. " + expect_ptr_id + " vs " + again_ptr_id);
61*795d594fSAndroid Build Coastguard Worker     } else {
62*795d594fSAndroid Build Coastguard Worker       System.out.println("pointer ID remains a pointer!");
63*795d594fSAndroid Build Coastguard Worker     }
64*795d594fSAndroid Build Coastguard Worker   }
65*795d594fSAndroid Build Coastguard Worker 
GetJniType()66*795d594fSAndroid Build Coastguard Worker   private static native String GetJniType();
SetToPointerIds()67*795d594fSAndroid Build Coastguard Worker   private static native void SetToPointerIds();
GetMethodId(boolean is_static, Class k, String name, String sig)68*795d594fSAndroid Build Coastguard Worker   private static native long GetMethodId(boolean is_static, Class k, String name, String sig);
69*795d594fSAndroid Build Coastguard Worker }
70