xref: /aosp_15_r20/art/test/2264-throwing-systemcleaner/src-art/Main.java (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2023 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 android.system.SystemCleaner;
18*795d594fSAndroid Build Coastguard Worker import dalvik.system.VMRuntime;
19*795d594fSAndroid Build Coastguard Worker import java.lang.ref.Cleaner;
20*795d594fSAndroid Build Coastguard Worker import java.util.concurrent.CountDownLatch;
21*795d594fSAndroid Build Coastguard Worker 
22*795d594fSAndroid Build Coastguard Worker import static java.util.concurrent.TimeUnit.MINUTES;
23*795d594fSAndroid Build Coastguard Worker 
24*795d594fSAndroid Build Coastguard Worker /**
25*795d594fSAndroid Build Coastguard Worker  * Test SystemCleaner with a bad cleaning action.
26*795d594fSAndroid Build Coastguard Worker  *
27*795d594fSAndroid Build Coastguard Worker  * This test is inherently very slightly flaky. It assumes that the system will schedule the
28*795d594fSAndroid Build Coastguard Worker  * finalizer daemon and finalizer watchdog daemon soon and often enough to reach the timeout and
29*795d594fSAndroid Build Coastguard Worker  * throw the fatal exception before we time out. Since we build in a 20 second buffer, failures
30*795d594fSAndroid Build Coastguard Worker  * should be very rare.
31*795d594fSAndroid Build Coastguard Worker  */
32*795d594fSAndroid Build Coastguard Worker public class Main {
33*795d594fSAndroid Build Coastguard Worker     static volatile Thread throwingThread;
main(String[] args)34*795d594fSAndroid Build Coastguard Worker     public static void main(String[] args) throws Exception {
35*795d594fSAndroid Build Coastguard Worker         Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
36*795d594fSAndroid Build Coastguard Worker             @Override
37*795d594fSAndroid Build Coastguard Worker             public void uncaughtException(Thread t, Throwable e) {
38*795d594fSAndroid Build Coastguard Worker                 if (t != throwingThread) {
39*795d594fSAndroid Build Coastguard Worker                   System.out.println("Exception from wrong thread");
40*795d594fSAndroid Build Coastguard Worker                 }
41*795d594fSAndroid Build Coastguard Worker                 if (!(e instanceof AssertionError)) {
42*795d594fSAndroid Build Coastguard Worker                   System.out.println("Unexpected uncaught exception");
43*795d594fSAndroid Build Coastguard Worker                 }
44*795d594fSAndroid Build Coastguard Worker                 System.out.println("Handling uncaught exception");
45*795d594fSAndroid Build Coastguard Worker                 System.exit(2);
46*795d594fSAndroid Build Coastguard Worker             }
47*795d594fSAndroid Build Coastguard Worker         });
48*795d594fSAndroid Build Coastguard Worker 
49*795d594fSAndroid Build Coastguard Worker         CountDownLatch cleanerWait1 = new CountDownLatch(1);
50*795d594fSAndroid Build Coastguard Worker         registerBadCleaner(Cleaner.create(), cleanerWait1);
51*795d594fSAndroid Build Coastguard Worker         gcRepeatedly();
52*795d594fSAndroid Build Coastguard Worker 
53*795d594fSAndroid Build Coastguard Worker         // Now wait for the finalizer to start running. Give it a minute.
54*795d594fSAndroid Build Coastguard Worker         cleanerWait1.await(1, MINUTES);
55*795d594fSAndroid Build Coastguard Worker 
56*795d594fSAndroid Build Coastguard Worker         // Give the cleaner Runnable a chance to finish running. That should do nothing,
57*795d594fSAndroid Build Coastguard Worker         // since exceptions from standard Cleaners are ignored.
58*795d594fSAndroid Build Coastguard Worker         snooze(10000);
59*795d594fSAndroid Build Coastguard Worker 
60*795d594fSAndroid Build Coastguard Worker         System.out.println("Good: Survived first exception");
61*795d594fSAndroid Build Coastguard Worker         // Now repeat with SystemCleaner.
62*795d594fSAndroid Build Coastguard Worker         CountDownLatch cleanerWait2 = new CountDownLatch(1);
63*795d594fSAndroid Build Coastguard Worker         registerBadCleaner(SystemCleaner.cleaner(), cleanerWait2);
64*795d594fSAndroid Build Coastguard Worker         gcRepeatedly();
65*795d594fSAndroid Build Coastguard Worker         cleanerWait2.await(1, MINUTES);
66*795d594fSAndroid Build Coastguard Worker 
67*795d594fSAndroid Build Coastguard Worker         // Now fall asleep. The timeout is large enough that we expect the
68*795d594fSAndroid Build Coastguard Worker         // FinalizerDaemon to have been killed by the exception.
69*795d594fSAndroid Build Coastguard Worker         // The timeout is also large enough to cover the extra 5 seconds we wait
70*795d594fSAndroid Build Coastguard Worker         // to dump on termination, plus potentially substantial gcstress overhead.
71*795d594fSAndroid Build Coastguard Worker         // Note: the timeout is here (instead of an infinite sleep) to protect the test
72*795d594fSAndroid Build Coastguard Worker         //       environment (e.g., in case this is run without a timeout wrapper).
73*795d594fSAndroid Build Coastguard Worker         snooze(20_000);
74*795d594fSAndroid Build Coastguard Worker 
75*795d594fSAndroid Build Coastguard Worker         // We should not get here.
76*795d594fSAndroid Build Coastguard Worker         System.out.println("Bad: Unexpectedly survived second exception");
77*795d594fSAndroid Build Coastguard Worker         System.exit(0);
78*795d594fSAndroid Build Coastguard Worker     }
79*795d594fSAndroid Build Coastguard Worker 
gcRepeatedly()80*795d594fSAndroid Build Coastguard Worker     private static void gcRepeatedly() {
81*795d594fSAndroid Build Coastguard Worker         // Should have at least two iterations to trigger finalization, but just to make sure run
82*795d594fSAndroid Build Coastguard Worker         // some more.
83*795d594fSAndroid Build Coastguard Worker         for (int i = 0; i < 5; i++) {
84*795d594fSAndroid Build Coastguard Worker             Runtime.getRuntime().gc();
85*795d594fSAndroid Build Coastguard Worker         }
86*795d594fSAndroid Build Coastguard Worker     }
87*795d594fSAndroid Build Coastguard Worker 
registerBadCleaner(Cleaner cleaner, CountDownLatch cleanerWait)88*795d594fSAndroid Build Coastguard Worker     private static void registerBadCleaner(Cleaner cleaner, CountDownLatch cleanerWait) {
89*795d594fSAndroid Build Coastguard Worker         Object obj = new Object();
90*795d594fSAndroid Build Coastguard Worker         cleaner.register(obj, () -> throwingCleanup(cleanerWait));
91*795d594fSAndroid Build Coastguard Worker         System.out.println("About to null reference.");
92*795d594fSAndroid Build Coastguard Worker         obj = null;  // Not that this would make a difference, could be eliminated earlier.
93*795d594fSAndroid Build Coastguard Worker     }
94*795d594fSAndroid Build Coastguard Worker 
snooze(int ms)95*795d594fSAndroid Build Coastguard Worker     public static void snooze(int ms) {
96*795d594fSAndroid Build Coastguard Worker         try {
97*795d594fSAndroid Build Coastguard Worker             Thread.sleep(ms);
98*795d594fSAndroid Build Coastguard Worker         } catch (InterruptedException ie) {
99*795d594fSAndroid Build Coastguard Worker             System.out.println("Unexpected interrupt");
100*795d594fSAndroid Build Coastguard Worker         }
101*795d594fSAndroid Build Coastguard Worker     }
102*795d594fSAndroid Build Coastguard Worker 
throwingCleanup(CountDownLatch cleanerWait)103*795d594fSAndroid Build Coastguard Worker     private static void throwingCleanup(CountDownLatch cleanerWait) {
104*795d594fSAndroid Build Coastguard Worker         throwingThread = Thread.currentThread();
105*795d594fSAndroid Build Coastguard Worker         cleanerWait.countDown();
106*795d594fSAndroid Build Coastguard Worker         System.out.println("Cleaner started and sleeping briefly...");
107*795d594fSAndroid Build Coastguard Worker         snooze(100);
108*795d594fSAndroid Build Coastguard Worker         throw new AssertionError("Process-killing exception");
109*795d594fSAndroid Build Coastguard Worker     }
110*795d594fSAndroid Build Coastguard Worker }
111