xref: /aosp_15_r20/art/test/2041-bad-cleaner/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 sun.misc.Cleaner;
19*795d594fSAndroid Build Coastguard Worker import java.util.concurrent.CountDownLatch;
20*795d594fSAndroid Build Coastguard Worker import static java.util.concurrent.TimeUnit.MINUTES;
21*795d594fSAndroid Build Coastguard Worker 
22*795d594fSAndroid Build Coastguard Worker /**
23*795d594fSAndroid Build Coastguard Worker  * Test a class with a bad finalizer.
24*795d594fSAndroid Build Coastguard Worker  *
25*795d594fSAndroid Build Coastguard Worker  * This test is inherently flaky. It assumes that the system will schedule the finalizer daemon
26*795d594fSAndroid Build Coastguard Worker  * and finalizer watchdog daemon enough to reach the timeout and throwing the fatal exception.
27*795d594fSAndroid Build Coastguard Worker  */
28*795d594fSAndroid Build Coastguard Worker public class Main {
main(String[] args)29*795d594fSAndroid Build Coastguard Worker     public static void main(String[] args) throws Exception {
30*795d594fSAndroid Build Coastguard Worker         CountDownLatch finalizerWait = new CountDownLatch(1);
31*795d594fSAndroid Build Coastguard Worker 
32*795d594fSAndroid Build Coastguard Worker         // A separate method to ensure no dex register keeps the object alive.
33*795d594fSAndroid Build Coastguard Worker         createBadCleanable(finalizerWait);
34*795d594fSAndroid Build Coastguard Worker 
35*795d594fSAndroid Build Coastguard Worker         // Should have at least two iterations to trigger finalization, but just to make sure run
36*795d594fSAndroid Build Coastguard Worker         // some more.
37*795d594fSAndroid Build Coastguard Worker         for (int i = 0; i < 5; i++) {
38*795d594fSAndroid Build Coastguard Worker             Runtime.getRuntime().gc();
39*795d594fSAndroid Build Coastguard Worker         }
40*795d594fSAndroid Build Coastguard Worker 
41*795d594fSAndroid Build Coastguard Worker         // Now wait for the finalizer to start running. Give it a minute.
42*795d594fSAndroid Build Coastguard Worker         finalizerWait.await(1, MINUTES);
43*795d594fSAndroid Build Coastguard Worker 
44*795d594fSAndroid Build Coastguard Worker         // Now fall asleep with a timeout. The timeout is large enough that we expect the
45*795d594fSAndroid Build Coastguard Worker         // finalizer daemon to have killed the process before the deadline elapses.
46*795d594fSAndroid Build Coastguard Worker         // The timeout is also large enough to cover the extra 5 seconds we wait
47*795d594fSAndroid Build Coastguard Worker         // to dump threads, plus potentially substantial gcstress overhead.
48*795d594fSAndroid Build Coastguard Worker         // The RQ timeout is currently effectively 5 * the finalizer timeout.
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 + 5 * 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 
createBadCleanable(CountDownLatch finalizerWait)70*795d594fSAndroid Build Coastguard Worker     private static void createBadCleanable(CountDownLatch finalizerWait) {
71*795d594fSAndroid Build Coastguard Worker         Object badCleanable = new Object();
72*795d594fSAndroid Build Coastguard Worker         Runnable badRunnable = new Runnable() {
73*795d594fSAndroid Build Coastguard Worker             public void run () {
74*795d594fSAndroid Build Coastguard Worker                 finalizerWait.countDown();
75*795d594fSAndroid Build Coastguard Worker 
76*795d594fSAndroid Build Coastguard Worker                 System.out.println("Cleaner started and sleeping briefly...");
77*795d594fSAndroid Build Coastguard Worker 
78*795d594fSAndroid Build Coastguard Worker                 snooze(2000);
79*795d594fSAndroid Build Coastguard Worker                 System.out.println("Cleaner done snoozing.");
80*795d594fSAndroid Build Coastguard Worker 
81*795d594fSAndroid Build Coastguard Worker                 System.out.println("Cleaner sleeping forever now.");
82*795d594fSAndroid Build Coastguard Worker                 while (true) {
83*795d594fSAndroid Build Coastguard Worker                     snooze(500000);
84*795d594fSAndroid Build Coastguard Worker                 }
85*795d594fSAndroid Build Coastguard Worker             }
86*795d594fSAndroid Build Coastguard Worker         };
87*795d594fSAndroid Build Coastguard Worker         final Cleaner badCleaner = Cleaner.create(badCleanable, badRunnable);
88*795d594fSAndroid Build Coastguard Worker 
89*795d594fSAndroid Build Coastguard Worker         System.out.println("About to null reference.");
90*795d594fSAndroid Build Coastguard Worker         badCleanable = null;  // Not that this would make a difference, could be eliminated earlier.
91*795d594fSAndroid Build Coastguard Worker     }
92*795d594fSAndroid Build Coastguard Worker 
snooze(int ms)93*795d594fSAndroid Build Coastguard Worker     public static void snooze(int ms) {
94*795d594fSAndroid Build Coastguard Worker         try {
95*795d594fSAndroid Build Coastguard Worker             Thread.sleep(ms);
96*795d594fSAndroid Build Coastguard Worker         } catch (InterruptedException ie) {
97*795d594fSAndroid Build Coastguard Worker         }
98*795d594fSAndroid Build Coastguard Worker     }
99*795d594fSAndroid Build Coastguard Worker 
100*795d594fSAndroid Build Coastguard Worker }
101