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.lang.reflect.Method;
18 import java.util.concurrent.atomic.AtomicInteger;
19 
20 public class Main {
21 
22   // Ensure that the "loop" method is compiled. Otherwise we currently have no real way to get rid
23   // of dead references. Return true if it looks like we succeeded.
ensureCompiled(Class cls, String methodName)24   public static boolean ensureCompiled(Class cls, String methodName) throws NoSuchMethodException {
25     Method m = cls.getDeclaredMethod(methodName);
26     if (isAotCompiled(cls, methodName)) {
27       return true;
28     } else {
29       ensureMethodJitCompiled(m);
30       if (hasJitCompiledEntrypoint(cls, methodName)) {
31         return true;
32       }
33       return false;
34     }
35   }
36 
37   // Garbage collect and check that the atomic counter has the expected value.
38   // Exped value of -1 means don't care.
39   // Noinline because we don't want the inlining here to interfere with the ReachabilitySensitive
40   // analysis.
$noinline$gcAndCheck(AtomicInteger counter, int expected, String label, String msg)41   public static void $noinline$gcAndCheck(AtomicInteger counter, int expected, String label,
42                                           String msg) {
43     Runtime.getRuntime().gc();
44     System.runFinalization();
45     int count = counter.get();
46     System.out.println(label + " count: " + count);
47     if (counter.get() != expected && expected != -1) {
48       System.out.println(msg);
49     }
50   }
51 
main(String[] args)52   public static void main(String[] args) {
53     System.loadLibrary(args[0]);
54     // Run several variations of the same test with different reachability annotations, etc.
55     // Only the DeadReferenceSafeTest should finalize every previously allocated object.
56     DeadReferenceUnsafeTest.runTest();
57     DeadReferenceSafeTest.runTest();
58     ReachabilitySensitiveTest.runTest();
59     ReachabilitySensitiveFunTest.runTest();
60     ReachabilityFenceTest.runTest();
61   }
ensureMethodJitCompiled(Method meth)62   public static native void ensureMethodJitCompiled(Method meth);
hasJitCompiledEntrypoint(Class<?> cls, String methodName)63   public static native boolean hasJitCompiledEntrypoint(Class<?> cls, String methodName);
isAotCompiled(Class<?> cls, String methodName)64   public static native boolean isAotCompiled(Class<?> cls, String methodName);
65 }
66