| Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 16 | |
| Hans Boehm | b215557 | 2019-03-27 14:25:53 -0700 | [diff] [blame] | 17 | import dalvik.system.VMRuntime; |
| Andreas Gampe | 6bbeecc | 2016-11-10 14:09:05 -0800 | [diff] [blame] | 18 | import java.util.concurrent.CountDownLatch; |
| 19 | import static java.util.concurrent.TimeUnit.MINUTES; |
| 20 | |
| jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 21 | /** |
| 22 | * Test a class with a bad finalizer. |
| Andreas Gampe | 6bbeecc | 2016-11-10 14:09:05 -0800 | [diff] [blame] | 23 | * |
| 24 | * This test is inherently flaky. It assumes that the system will schedule the finalizer daemon |
| 25 | * and finalizer watchdog daemon enough to reach the timeout and throwing the fatal exception. |
| jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 26 | */ |
| 27 | public class Main { |
| Andreas Gampe | 6bbeecc | 2016-11-10 14:09:05 -0800 | [diff] [blame] | 28 | public static void main(String[] args) throws Exception { |
| 29 | CountDownLatch finalizerWait = new CountDownLatch(1); |
| jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 30 | |
| Andreas Gampe | 6bbeecc | 2016-11-10 14:09:05 -0800 | [diff] [blame] | 31 | // A separate method to ensure no dex register keeps the object alive. |
| 32 | createBadFinalizer(finalizerWait); |
| jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 33 | |
| Andreas Gampe | 6bbeecc | 2016-11-10 14:09:05 -0800 | [diff] [blame] | 34 | // Should have at least two iterations to trigger finalization, but just to make sure run |
| 35 | // some more. |
| 36 | for (int i = 0; i < 5; i++) { |
| Mathieu Chartier | 7befd0e | 2014-02-03 17:48:41 -0800 | [diff] [blame] | 37 | Runtime.getRuntime().gc(); |
| jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| Andreas Gampe | 6bbeecc | 2016-11-10 14:09:05 -0800 | [diff] [blame] | 40 | // Now wait for the finalizer to start running. Give it a minute. |
| 41 | finalizerWait.await(1, MINUTES); |
| 42 | |
| 43 | // Now fall asleep with a timeout. The timeout is large enough that we expect the |
| 44 | // finalizer daemon to have killed the process before the deadline elapses. |
| Hans Boehm | b215557 | 2019-03-27 14:25:53 -0700 | [diff] [blame] | 45 | // The timeout is also large enough to cover the extra 5 seconds we wait |
| 46 | // to dump threads, plus potentially substantial gcstress overhead. |
| Andreas Gampe | 6bbeecc | 2016-11-10 14:09:05 -0800 | [diff] [blame] | 47 | // Note: the timeout is here (instead of an infinite sleep) to protect the test |
| 48 | // environment (e.g., in case this is run without a timeout wrapper). |
| Hans Boehm | b215557 | 2019-03-27 14:25:53 -0700 | [diff] [blame] | 49 | final long timeout = 100 * 1000 + VMRuntime.getRuntime().getFinalizerTimeoutMs(); |
| Andreas Gampe | 6bbeecc | 2016-11-10 14:09:05 -0800 | [diff] [blame] | 50 | long remainingWait = timeout; |
| 51 | final long waitStart = System.currentTimeMillis(); |
| 52 | while (remainingWait > 0) { |
| 53 | synchronized (args) { // Just use an already existing object for simplicity... |
| 54 | try { |
| 55 | args.wait(remainingWait); |
| 56 | } catch (Exception e) { |
| 57 | } |
| 58 | } |
| 59 | remainingWait = timeout - (System.currentTimeMillis() - waitStart); |
| 60 | } |
| 61 | |
| 62 | // We should not get here. |
| Brian Carlstrom | bca77e6 | 2014-02-20 14:02:48 -0800 | [diff] [blame] | 63 | System.out.println("UNREACHABLE"); |
| jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 64 | System.exit(0); |
| 65 | } |
| Brian Carlstrom | bca77e6 | 2014-02-20 14:02:48 -0800 | [diff] [blame] | 66 | |
| Andreas Gampe | 6bbeecc | 2016-11-10 14:09:05 -0800 | [diff] [blame] | 67 | private static void createBadFinalizer(CountDownLatch finalizerWait) { |
| 68 | BadFinalizer bf = new BadFinalizer(finalizerWait); |
| 69 | |
| 70 | System.out.println("About to null reference."); |
| 71 | bf = null; // Not that this would make a difference, could be eliminated earlier. |
| 72 | } |
| 73 | |
| Brian Carlstrom | bca77e6 | 2014-02-20 14:02:48 -0800 | [diff] [blame] | 74 | public static void snooze(int ms) { |
| 75 | try { |
| 76 | Thread.sleep(ms); |
| 77 | } catch (InterruptedException ie) { |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Class with a bad finalizer. |
| 83 | */ |
| 84 | public static class BadFinalizer { |
| Andreas Gampe | 6bbeecc | 2016-11-10 14:09:05 -0800 | [diff] [blame] | 85 | private CountDownLatch finalizerWait; |
| 86 | private volatile int j = 0; // Volatile in an effort to curb loop optimization. |
| 87 | |
| 88 | public BadFinalizer(CountDownLatch finalizerWait) { |
| 89 | this.finalizerWait = finalizerWait; |
| 90 | } |
| 91 | |
| Brian Carlstrom | bca77e6 | 2014-02-20 14:02:48 -0800 | [diff] [blame] | 92 | protected void finalize() { |
| Andreas Gampe | 6bbeecc | 2016-11-10 14:09:05 -0800 | [diff] [blame] | 93 | finalizerWait.countDown(); |
| 94 | |
| Hans Boehm | 7541fda | 2021-09-29 03:31:59 +0000 | [diff] [blame^] | 95 | System.out.println("Finalizer started and spinning..."); |
| Brian Carlstrom | bca77e6 | 2014-02-20 14:02:48 -0800 | [diff] [blame] | 96 | |
| Hans Boehm | 7541fda | 2021-09-29 03:31:59 +0000 | [diff] [blame^] | 97 | /* spin for a bit */ |
| Brian Carlstrom | bca77e6 | 2014-02-20 14:02:48 -0800 | [diff] [blame] | 98 | long start, end; |
| 99 | start = System.nanoTime(); |
| Goran Jakovljevic | de66d03 | 2017-04-27 12:04:25 +0200 | [diff] [blame] | 100 | snooze(2000); |
| Brian Carlstrom | bca77e6 | 2014-02-20 14:02:48 -0800 | [diff] [blame] | 101 | end = System.nanoTime(); |
| Hans Boehm | 7541fda | 2021-09-29 03:31:59 +0000 | [diff] [blame^] | 102 | System.out.println("Finalizer done spinning."); |
| Brian Carlstrom | bca77e6 | 2014-02-20 14:02:48 -0800 | [diff] [blame] | 103 | |
| 104 | System.out.println("Finalizer sleeping forever now."); |
| 105 | while (true) { |
| 106 | snooze(10000); |
| 107 | } |
| 108 | } |
| 109 | } |
| jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 110 | } |