1 /* 2 * Copyright (C) 2022 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 public class Main { 18 static Main empty; 19 20 static class MyThread extends Thread { run()21 public void run() { 22 // This will throw at `callMethodThatThrows` and trigger deoptimization checks which we used 23 // to crash on. 24 new Inner(); 25 } 26 } 27 28 public static class Inner { 29 // Have a <clinit> method invoke another <clinit> method to ensure we execute in the 30 // interpreter. 31 static { Inner2()32 new Inner2(); 33 } 34 } 35 36 public static class Inner2 { 37 static { Main.callMethodThatThrows()38 Main.callMethodThatThrows(); 39 } 40 } 41 main(String[] args)42 public static void main(String[] args) throws Exception { 43 System.loadLibrary(args[0]); 44 // Disables use of nterp. 45 Main.setAsyncExceptionsThrown(); 46 47 // Execute the test in a different thread, to ensure we still 48 // return a 0 exit status. 49 Thread t = new MyThread(); 50 t.setUncaughtExceptionHandler((th, e) -> { 51 System.out.println("Caught exception"); 52 }); 53 t.start(); 54 t.join(); 55 } 56 callMethodThatThrows()57 public static void callMethodThatThrows() { 58 // Ensures we get deoptimization requests. 59 Main.forceInterpreterOnThread(); 60 throw new Error(""); 61 } 62 forceInterpreterOnThread()63 public static native void forceInterpreterOnThread(); setAsyncExceptionsThrown()64 public static native void setAsyncExceptionsThrown(); 65 66 } 67