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