• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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 
17 import java.util.function.Consumer;
18 
19 public class Main {
20   public static final boolean PRINT = false;
21 
22   public static class PtrCls {
doNothingPtr()23     public static void doNothingPtr() {}
24   }
25 
26   public static class IdxCls {
doNothingIdx()27     public static void doNothingIdx() {}
28   }
29 
DbgPrint(String str)30   public static void DbgPrint(String str) {
31     if (PRINT) {
32       System.out.println(str);
33     }
34   }
35 
GetId(Class<?> k, String name)36   public static long GetId(Class<?> k, String name) {
37     return GetMethodId(true, k, name, "()V");
38   }
39 
main(String[] args)40   public static void main(String[] args) {
41     System.loadLibrary(args[0]);
42     System.out.println("JNI Type is: " + GetJniType());
43     long expect_ptr_id = GetId(PtrCls.class, "doNothingPtr");
44     DbgPrint(String.format("expected_ptr_id is 0x%x", expect_ptr_id));
45     if (expect_ptr_id % 4 != 0) {
46       throw new Error("ID " + expect_ptr_id + " is not aligned!");
47     } else {
48       System.out.println("pointer ID looks like a pointer!");
49     }
50     SetToIndexIds();
51     System.out.println("JNI Type is: " + GetJniType());
52     long expect_idx_id = GetId(IdxCls.class, "doNothingIdx");
53     DbgPrint(String.format("expected_idx_id is 0x%x", expect_idx_id));
54     if (expect_idx_id % 2 != 1) {
55       throw new Error("ID " + expect_ptr_id + " is not odd!");
56     } else {
57       System.out.println("index ID looks like an index!");
58     }
59     long again_ptr_id = GetId(PtrCls.class, "doNothingPtr");
60     if (expect_ptr_id != again_ptr_id) {
61       throw new Error(
62           "Got different id values for same method. " + expect_ptr_id + " vs " + again_ptr_id);
63     } else {
64       System.out.println("pointer ID remains a pointer!");
65     }
66     long well_known_id = GetMethodId(false, Consumer.class, "accept", "(Ljava/lang/Object;)V");
67     DbgPrint(String.format("well_known_id is 0x%x", well_known_id));
68     if (well_known_id % 2 != 1) {
69       throw new Error("WKC ID " + well_known_id + " is not odd!");
70     } else {
71       System.out.println("index WKC ID looks like an index!");
72     }
73   }
74 
GetJniType()75   private static native String GetJniType();
SetToIndexIds()76   private static native void SetToIndexIds();
GetMethodId(boolean is_static, Class k, String name, String sig)77   private static native long GetMethodId(boolean is_static, Class k, String name, String sig);
78 }
79