xref: /aosp_15_r20/art/test/680-checker-deopt-dex-pc-0/src/Main.java (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2018 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 public class Main {
18*795d594fSAndroid Build Coastguard Worker     // We run this test for AOT to verify that there is a HDeoptimize with dex pc 0.
19*795d594fSAndroid Build Coastguard Worker     /// CHECK-START: int Main.$noinline$getInt(byte[], int) BCE (after)
20*795d594fSAndroid Build Coastguard Worker     /// CHECK:          Deoptimize dex_pc:0
$noinline$getInt(byte[] array, int offset)21*795d594fSAndroid Build Coastguard Worker     public static int $noinline$getInt(byte[] array, int offset) {
22*795d594fSAndroid Build Coastguard Worker         // The aget for `array[offset]` is at dex pc 0, so the Deoptimize
23*795d594fSAndroid Build Coastguard Worker         // from dynamic BCE shall also be at dex pc 0.
24*795d594fSAndroid Build Coastguard Worker         return ((array[offset    ] & 0xFF) <<  0) +
25*795d594fSAndroid Build Coastguard Worker                ((array[offset + 1] & 0xFF) <<  8) +
26*795d594fSAndroid Build Coastguard Worker                ((array[offset + 2] & 0xFF) << 16) +
27*795d594fSAndroid Build Coastguard Worker                ((array[offset + 3] & 0xFF) << 24);
28*795d594fSAndroid Build Coastguard Worker     }
29*795d594fSAndroid Build Coastguard Worker 
main(String[] args)30*795d594fSAndroid Build Coastguard Worker     public static void main(String[] args) {
31*795d594fSAndroid Build Coastguard Worker         System.loadLibrary(args[0]);
32*795d594fSAndroid Build Coastguard Worker         if (hasJit()) {
33*795d594fSAndroid Build Coastguard Worker             byte[] array = { 0, 1, 2, 3 };
34*795d594fSAndroid Build Coastguard Worker             ensureJitCompiled(Main.class, "$noinline$getInt");
35*795d594fSAndroid Build Coastguard Worker             if (!hasJitCompiledEntrypoint(Main.class, "$noinline$getInt")) {
36*795d594fSAndroid Build Coastguard Worker                 throw new Error("Unexpected entrypoint!");
37*795d594fSAndroid Build Coastguard Worker             }
38*795d594fSAndroid Build Coastguard Worker             if ($noinline$getInt(array, 0) != 0x03020100) {
39*795d594fSAndroid Build Coastguard Worker                 throw new Error();
40*795d594fSAndroid Build Coastguard Worker             }
41*795d594fSAndroid Build Coastguard Worker             try {
42*795d594fSAndroid Build Coastguard Worker                 // The HDeoptimize at dex pc 0 was previously handled poorly as the dex pc 0
43*795d594fSAndroid Build Coastguard Worker                 // was used to detect whether we entered the method. This meant that the
44*795d594fSAndroid Build Coastguard Worker                 // instrumentation would have reported MethodEnteredEvent and we would have
45*795d594fSAndroid Build Coastguard Worker                 // told JIT that the method was entered. With JIT-on-first-use we would also
46*795d594fSAndroid Build Coastguard Worker                 // immediatelly recompile the method and run the compiled code leading to
47*795d594fSAndroid Build Coastguard Worker                 // a an infinite deoptimization recursion, yielding StackOverflowError.
48*795d594fSAndroid Build Coastguard Worker                 $noinline$getInt(array, 1);
49*795d594fSAndroid Build Coastguard Worker             } catch (ArrayIndexOutOfBoundsException ignored) {}
50*795d594fSAndroid Build Coastguard Worker         }
51*795d594fSAndroid Build Coastguard Worker         System.out.println("passed");
52*795d594fSAndroid Build Coastguard Worker     }
53*795d594fSAndroid Build Coastguard Worker 
hasJit()54*795d594fSAndroid Build Coastguard Worker     public static native boolean hasJit();
hasJitCompiledEntrypoint(Class<?> cls, String methodName)55*795d594fSAndroid Build Coastguard Worker     public native static boolean hasJitCompiledEntrypoint(Class<?> cls, String methodName);
ensureJitCompiled(Class<?> cls, String methodName)56*795d594fSAndroid Build Coastguard Worker     public native static void ensureJitCompiled(Class<?> cls, String methodName);
57*795d594fSAndroid Build Coastguard Worker }
58