blob: 72cb5aa86e0f8d443953cd584be6ce5dd4c49035 [file] [log] [blame]
Hans Boehm2007f852021-09-21 14:06:36 -07001/*
2 * Copyright (C) 2007 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
17import dalvik.system.VMRuntime;
18import sun.misc.Cleaner;
19import java.util.concurrent.CountDownLatch;
20import static java.util.concurrent.TimeUnit.MINUTES;
21
22/**
23 * Test a class with a bad finalizer.
24 *
25 * This test is inherently flaky. It assumes that the system will schedule the finalizer daemon
26 * and finalizer watchdog daemon enough to reach the timeout and throwing the fatal exception.
27 */
28public class Main {
29 public static void main(String[] args) throws Exception {
30 CountDownLatch finalizerWait = new CountDownLatch(1);
31
32 // A separate method to ensure no dex register keeps the object alive.
33 createBadCleanable(finalizerWait);
34
35 // Should have at least two iterations to trigger finalization, but just to make sure run
36 // some more.
37 for (int i = 0; i < 5; i++) {
38 Runtime.getRuntime().gc();
39 }
40
41 // Now wait for the finalizer to start running. Give it a minute.
42 finalizerWait.await(1, MINUTES);
43
44 // Now fall asleep with a timeout. The timeout is large enough that we expect the
45 // finalizer daemon to have killed the process before the deadline elapses.
46 // The timeout is also large enough to cover the extra 5 seconds we wait
47 // to dump threads, plus potentially substantial gcstress overhead.
48 // Note: the timeout is here (instead of an infinite sleep) to protect the test
49 // environment (e.g., in case this is run without a timeout wrapper).
50 final long timeout = 100 * 1000 + VMRuntime.getRuntime().getFinalizerTimeoutMs();
51 long remainingWait = timeout;
52 final long waitStart = System.currentTimeMillis();
53 while (remainingWait > 0) {
54 synchronized (args) { // Just use an already existing object for simplicity...
55 try {
56 args.wait(remainingWait);
57 } catch (Exception e) {
58 System.out.println("UNEXPECTED EXCEPTION");
59 }
60 }
61 remainingWait = timeout - (System.currentTimeMillis() - waitStart);
62 }
63
64 // We should not get here.
65 System.out.println("UNREACHABLE");
66 System.exit(0);
67 }
68
69 private static void createBadCleanable(CountDownLatch finalizerWait) {
70 Object badCleanable = new Object();
71 Runnable badRunnable = new Runnable() {
72 public void run () {
73 finalizerWait.countDown();
74
75 System.out.println("Cleaner started and sleeping briefly...");
76
77 snooze(2000);
78 System.out.println("Cleaner done snoozing.");
79
80 System.out.println("Cleaner sleeping forever now.");
81 while (true) {
82 snooze(500000);
83 }
84 }
85 };
86 final Cleaner badCleaner = Cleaner.create(badCleanable, badRunnable);
87
88 System.out.println("About to null reference.");
89 badCleanable = null; // Not that this would make a difference, could be eliminated earlier.
90 }
91
92 public static void snooze(int ms) {
93 try {
94 Thread.sleep(ms);
95 } catch (InterruptedException ie) {
96 }
97 }
98
99}