xref: /aosp_15_r20/art/test/2048-bad-native-registry/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 libcore.util.NativeAllocationRegistry;
20*795d594fSAndroid Build Coastguard Worker import java.util.concurrent.TimeoutException;
21*795d594fSAndroid Build Coastguard Worker import static java.util.concurrent.TimeUnit.MINUTES;
22*795d594fSAndroid Build Coastguard Worker 
23*795d594fSAndroid Build Coastguard Worker /**
24*795d594fSAndroid Build Coastguard Worker  * Test a class with a bad finalizer.
25*795d594fSAndroid Build Coastguard Worker  *
26*795d594fSAndroid Build Coastguard Worker  * This test is inherently flaky. It assumes that the system will schedule the finalizer daemon
27*795d594fSAndroid Build Coastguard Worker  * and finalizer watchdog daemon enough to reach the timeout and throwing the fatal exception.
28*795d594fSAndroid Build Coastguard Worker  * This uses somewhat simpler logic than 2041-bad-cleaner, since the handshake implemented there
29*795d594fSAndroid Build Coastguard Worker  * is harder to replicate here. We bump up the timeout below a bit to compensate.
30*795d594fSAndroid Build Coastguard Worker  */
31*795d594fSAndroid Build Coastguard Worker public class Main {
main(String[] args)32*795d594fSAndroid Build Coastguard Worker     public static void main(String[] args) throws Exception {
33*795d594fSAndroid Build Coastguard Worker         System.loadLibrary(args[0]);
34*795d594fSAndroid Build Coastguard Worker         ClassLoader cl = Main.class.getClassLoader();
35*795d594fSAndroid Build Coastguard Worker         NativeAllocationRegistry registry =
36*795d594fSAndroid Build Coastguard Worker             NativeAllocationRegistry.createNonmalloced(cl, getBadFreeFunction(), 666);
37*795d594fSAndroid Build Coastguard Worker         // Replace the global uncaught exception handler, so the exception shows up on stdout.
38*795d594fSAndroid Build Coastguard Worker         Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
39*795d594fSAndroid Build Coastguard Worker             public void uncaughtException(Thread thread, Throwable e) {
40*795d594fSAndroid Build Coastguard Worker                 if (e instanceof TimeoutException) {
41*795d594fSAndroid Build Coastguard Worker                     System.out.println("TimeoutException on "
42*795d594fSAndroid Build Coastguard Worker                         + thread.getName() + ":" + e.getMessage());
43*795d594fSAndroid Build Coastguard Worker                 } else {
44*795d594fSAndroid Build Coastguard Worker                     System.out.println("Unexpected exception " + e);
45*795d594fSAndroid Build Coastguard Worker                 }
46*795d594fSAndroid Build Coastguard Worker                 System.exit(2);
47*795d594fSAndroid Build Coastguard Worker             }
48*795d594fSAndroid Build Coastguard Worker         });
49*795d594fSAndroid Build Coastguard Worker         // A separate method to ensure no dex register keeps the object alive.
50*795d594fSAndroid Build Coastguard Worker         createBadCleanable(registry);
51*795d594fSAndroid Build Coastguard Worker 
52*795d594fSAndroid Build Coastguard Worker         // Should have at least two iterations to trigger finalization, but just to make sure run
53*795d594fSAndroid Build Coastguard Worker         // some more.
54*795d594fSAndroid Build Coastguard Worker         for (int i = 0; i < 5; i++) {
55*795d594fSAndroid Build Coastguard Worker             Runtime.getRuntime().gc();
56*795d594fSAndroid Build Coastguard Worker         }
57*795d594fSAndroid Build Coastguard Worker 
58*795d594fSAndroid Build Coastguard Worker         // Now fall asleep with a timeout. The timeout is large enough that we expect the
59*795d594fSAndroid Build Coastguard Worker         // finalizer daemon to have killed the process before the deadline elapses.
60*795d594fSAndroid Build Coastguard Worker         // The timeout is also large enough to cover the extra 5 seconds we wait
61*795d594fSAndroid Build Coastguard Worker         // to dump threads, plus potentially substantial gcstress overhead.
62*795d594fSAndroid Build Coastguard Worker         // The RQ timeout is currently effectively 5 * the finalizer timeout.
63*795d594fSAndroid Build Coastguard Worker         // Note: the timeout is here (instead of an infinite sleep) to protect the test
64*795d594fSAndroid Build Coastguard Worker         //       environment (e.g., in case this is run without a timeout wrapper).
65*795d594fSAndroid Build Coastguard Worker         final long timeout = 150 * 1000 + 5 * VMRuntime.getRuntime().getFinalizerTimeoutMs();
66*795d594fSAndroid Build Coastguard Worker         long remainingWait = timeout;
67*795d594fSAndroid Build Coastguard Worker         final long waitStart = System.currentTimeMillis();
68*795d594fSAndroid Build Coastguard Worker         while (remainingWait > 0) {
69*795d594fSAndroid Build Coastguard Worker             synchronized (args) {  // Just use an already existing object for simplicity...
70*795d594fSAndroid Build Coastguard Worker                 try {
71*795d594fSAndroid Build Coastguard Worker                     args.wait(remainingWait);
72*795d594fSAndroid Build Coastguard Worker                 } catch (Exception e) {
73*795d594fSAndroid Build Coastguard Worker                     System.out.println("UNEXPECTED EXCEPTION");
74*795d594fSAndroid Build Coastguard Worker                 }
75*795d594fSAndroid Build Coastguard Worker             }
76*795d594fSAndroid Build Coastguard Worker             remainingWait = timeout - (System.currentTimeMillis() - waitStart);
77*795d594fSAndroid Build Coastguard Worker         }
78*795d594fSAndroid Build Coastguard Worker 
79*795d594fSAndroid Build Coastguard Worker         // We should not get here.
80*795d594fSAndroid Build Coastguard Worker         System.out.println("UNREACHABLE");
81*795d594fSAndroid Build Coastguard Worker         System.exit(0);
82*795d594fSAndroid Build Coastguard Worker     }
83*795d594fSAndroid Build Coastguard Worker 
createBadCleanable(NativeAllocationRegistry registry)84*795d594fSAndroid Build Coastguard Worker     private static void createBadCleanable(NativeAllocationRegistry registry) {
85*795d594fSAndroid Build Coastguard Worker         Object badCleanable = new Object();
86*795d594fSAndroid Build Coastguard Worker         long nativeObj = getNativeObj();
87*795d594fSAndroid Build Coastguard Worker         registry.registerNativeAllocation(badCleanable, nativeObj);
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 
getNativeObj()93*795d594fSAndroid Build Coastguard Worker     private static native long getNativeObj();
getBadFreeFunction()94*795d594fSAndroid Build Coastguard Worker     private static native long getBadFreeFunction();
95*795d594fSAndroid Build Coastguard Worker 
96*795d594fSAndroid Build Coastguard Worker }
97