1 /*
2  * Copyright (C) 2018 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 
17 import java.lang.reflect.Field;
18 import java.lang.reflect.Method;
19 
20 public class ChildClass {
21   // This method being synchronized means the SIGQUIT code in ART will call
22   // FindLocksAtDexPc (we check for the presence of try blocks),
23   // which triggered a DCHECK of an invariant.
runTest()24   public static synchronized void runTest() throws Exception {
25     Main m = Foo.mainObject;
26 
27     SigQuit.doKill();
28 
29     // Sleep some time to get the kill while executing this method.
30     Thread.sleep(2);
31 
32     // The FindLocksAtDexPc method running with the verifier would fail when
33     // resolving this call, as the verifier didn't register Main from the field
34     // access above with the current class loader.
35     Main.staticMethod();
36     System.out.println("Done");
37   }
38 
39   private final static class SigQuit {
40     private final static int sigquit;
41     private final static Method kill;
42     private final static int pid;
43 
44     static {
45       int pidTemp = -1;
46       int sigquitTemp = -1;
47       Method killTemp = null;
48 
49       try {
50         Class<?> osClass = Class.forName("android.system.Os");
51         Method getpid = osClass.getDeclaredMethod("getpid");
52         pidTemp = (Integer)getpid.invoke(null);
53 
54         Class<?> osConstants = Class.forName("android.system.OsConstants");
55         Field sigquitField = osConstants.getDeclaredField("SIGQUIT");
56         sigquitTemp = (Integer)sigquitField.get(null);
57 
58         killTemp = osClass.getDeclaredMethod("kill", int.class, int.class);
59       } catch (Exception e) {
60         throw new Error(e);
61       }
62 
63       pid = pidTemp;
64       sigquit = sigquitTemp;
65       kill = killTemp;
66     }
67 
doKill()68     public static void doKill() throws Exception {
69       kill.invoke(null, pid, sigquit);
70     }
71   }
72 }
73