xref: /aosp_15_r20/art/test/030-bad-finalizer/src/Main.java (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2007 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 dalvik.system.VMRuntime;
18*795d594fSAndroid Build Coastguard Worker import java.util.concurrent.CountDownLatch;
19*795d594fSAndroid Build Coastguard Worker import static java.util.concurrent.TimeUnit.MINUTES;
20*795d594fSAndroid Build Coastguard Worker 
21*795d594fSAndroid Build Coastguard Worker /**
22*795d594fSAndroid Build Coastguard Worker  * Test a class with a bad finalizer.
23*795d594fSAndroid Build Coastguard Worker  *
24*795d594fSAndroid Build Coastguard Worker  * This test is inherently very slightly flaky. It assumes that the system will schedule the
25*795d594fSAndroid Build Coastguard Worker  * finalizer daemon and finalizer watchdog daemon soon and often enough to reach the timeout and
26*795d594fSAndroid Build Coastguard Worker  * throw the fatal exception before we time out. Since we build in a 100 second buffer, failures
27*795d594fSAndroid Build Coastguard Worker  * should be very rare.
28*795d594fSAndroid Build Coastguard Worker  */
29*795d594fSAndroid Build Coastguard Worker public class Main {
main(String[] args)30*795d594fSAndroid Build Coastguard Worker     public static void main(String[] args) throws Exception {
31*795d594fSAndroid Build Coastguard Worker         CountDownLatch finalizerWait = new CountDownLatch(1);
32*795d594fSAndroid Build Coastguard Worker 
33*795d594fSAndroid Build Coastguard Worker         // A separate method to ensure no dex register keeps the object alive.
34*795d594fSAndroid Build Coastguard Worker         createBadFinalizer(finalizerWait);
35*795d594fSAndroid Build Coastguard Worker 
36*795d594fSAndroid Build Coastguard Worker         // Should have at least two iterations to trigger finalization, but just to make sure run
37*795d594fSAndroid Build Coastguard Worker         // some more.
38*795d594fSAndroid Build Coastguard Worker         for (int i = 0; i < 5; i++) {
39*795d594fSAndroid Build Coastguard Worker             Runtime.getRuntime().gc();
40*795d594fSAndroid Build Coastguard Worker         }
41*795d594fSAndroid Build Coastguard Worker 
42*795d594fSAndroid Build Coastguard Worker         // Now wait for the finalizer to start running. Give it a minute.
43*795d594fSAndroid Build Coastguard Worker         finalizerWait.await(1, MINUTES);
44*795d594fSAndroid Build Coastguard Worker 
45*795d594fSAndroid Build Coastguard Worker         // Now fall asleep with a timeout. The timeout is large enough that we expect the
46*795d594fSAndroid Build Coastguard Worker         // finalizer daemon to have killed the process before the deadline elapses.
47*795d594fSAndroid Build Coastguard Worker         // The timeout is also large enough to cover the extra 5 seconds we wait
48*795d594fSAndroid Build Coastguard Worker         // to dump threads, plus potentially substantial gcstress overhead.
49*795d594fSAndroid Build Coastguard Worker         // Note: the timeout is here (instead of an infinite sleep) to protect the test
50*795d594fSAndroid Build Coastguard Worker         //       environment (e.g., in case this is run without a timeout wrapper).
51*795d594fSAndroid Build Coastguard Worker         final long timeout = 100 * 1000 + VMRuntime.getRuntime().getFinalizerTimeoutMs();
52*795d594fSAndroid Build Coastguard Worker         long remainingWait = timeout;
53*795d594fSAndroid Build Coastguard Worker         final long waitStart = System.currentTimeMillis();
54*795d594fSAndroid Build Coastguard Worker         while (remainingWait > 0) {
55*795d594fSAndroid Build Coastguard Worker             synchronized (args) {  // Just use an already existing object for simplicity...
56*795d594fSAndroid Build Coastguard Worker                 try {
57*795d594fSAndroid Build Coastguard Worker                     args.wait(remainingWait);
58*795d594fSAndroid Build Coastguard Worker                 } catch (Exception e) {
59*795d594fSAndroid Build Coastguard Worker                     System.out.println("UNEXPECTED EXCEPTION");
60*795d594fSAndroid Build Coastguard Worker                 }
61*795d594fSAndroid Build Coastguard Worker             }
62*795d594fSAndroid Build Coastguard Worker             remainingWait = timeout - (System.currentTimeMillis() - waitStart);
63*795d594fSAndroid Build Coastguard Worker         }
64*795d594fSAndroid Build Coastguard Worker 
65*795d594fSAndroid Build Coastguard Worker         // We should not get here.
66*795d594fSAndroid Build Coastguard Worker         System.out.println("UNREACHABLE");
67*795d594fSAndroid Build Coastguard Worker         System.exit(0);
68*795d594fSAndroid Build Coastguard Worker     }
69*795d594fSAndroid Build Coastguard Worker 
createBadFinalizer(CountDownLatch finalizerWait)70*795d594fSAndroid Build Coastguard Worker     private static void createBadFinalizer(CountDownLatch finalizerWait) {
71*795d594fSAndroid Build Coastguard Worker         BadFinalizer bf = new BadFinalizer(finalizerWait);
72*795d594fSAndroid Build Coastguard Worker 
73*795d594fSAndroid Build Coastguard Worker         System.out.println("About to null reference.");
74*795d594fSAndroid Build Coastguard Worker         bf = null;  // Not that this would make a difference, could be eliminated earlier.
75*795d594fSAndroid Build Coastguard Worker     }
76*795d594fSAndroid Build Coastguard Worker 
snooze(int ms)77*795d594fSAndroid Build Coastguard Worker     public static void snooze(int ms) {
78*795d594fSAndroid Build Coastguard Worker         try {
79*795d594fSAndroid Build Coastguard Worker             Thread.sleep(ms);
80*795d594fSAndroid Build Coastguard Worker         } catch (InterruptedException ie) {
81*795d594fSAndroid Build Coastguard Worker             System.out.println("Unexpected interrupt");
82*795d594fSAndroid Build Coastguard Worker         }
83*795d594fSAndroid Build Coastguard Worker     }
84*795d594fSAndroid Build Coastguard Worker 
85*795d594fSAndroid Build Coastguard Worker     /**
86*795d594fSAndroid Build Coastguard Worker      * Class with a bad finalizer.
87*795d594fSAndroid Build Coastguard Worker      */
88*795d594fSAndroid Build Coastguard Worker     public static class BadFinalizer {
89*795d594fSAndroid Build Coastguard Worker         private CountDownLatch finalizerWait;
90*795d594fSAndroid Build Coastguard Worker         private volatile int j = 0;  // Volatile in an effort to curb loop optimization.
91*795d594fSAndroid Build Coastguard Worker 
BadFinalizer(CountDownLatch finalizerWait)92*795d594fSAndroid Build Coastguard Worker         public BadFinalizer(CountDownLatch finalizerWait) {
93*795d594fSAndroid Build Coastguard Worker             this.finalizerWait = finalizerWait;
94*795d594fSAndroid Build Coastguard Worker         }
95*795d594fSAndroid Build Coastguard Worker 
finalize()96*795d594fSAndroid Build Coastguard Worker         protected void finalize() {
97*795d594fSAndroid Build Coastguard Worker             finalizerWait.countDown();
98*795d594fSAndroid Build Coastguard Worker 
99*795d594fSAndroid Build Coastguard Worker             System.out.println("Finalizer started and sleeping briefly...");
100*795d594fSAndroid Build Coastguard Worker 
101*795d594fSAndroid Build Coastguard Worker             long start, end;
102*795d594fSAndroid Build Coastguard Worker             start = System.nanoTime();
103*795d594fSAndroid Build Coastguard Worker             snooze(2000);
104*795d594fSAndroid Build Coastguard Worker             end = System.nanoTime();
105*795d594fSAndroid Build Coastguard Worker             System.out.println("Finalizer done snoozing.");
106*795d594fSAndroid Build Coastguard Worker 
107*795d594fSAndroid Build Coastguard Worker             System.out.println("Finalizer sleeping forever now.");
108*795d594fSAndroid Build Coastguard Worker             while (true) {
109*795d594fSAndroid Build Coastguard Worker                 snooze(10000);
110*795d594fSAndroid Build Coastguard Worker             }
111*795d594fSAndroid Build Coastguard Worker         }
112*795d594fSAndroid Build Coastguard Worker     }
113*795d594fSAndroid Build Coastguard Worker }
114