xref: /aosp_15_r20/art/test/1960-checker-bounds-codegen/src/Main.java (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1 /*
2  * Copyright (C) 2019 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 /**
18  * Test code generation for BoundsCheck.
19  */
20 public class Main {
21   // Constant index, variable length.
22   /// CHECK-START-ARM64: int Main.constantIndex(int[]) disassembly (after)
23   /// CHECK:                     BoundsCheck
24   /// CHECK:                     cmp {{w\d+}}, #0x0
25   /// CHECK:                     b.ls #+0x{{[0-9a-f]+}} (addr 0x<<SLOW:[0-9a-f]+>>)
26   /// CHECK:                     BoundsCheckSlowPathARM64
27   /// CHECK-NEXT:                0x<<SLOW>>:
28   /// CHECK-START-ARM: int Main.constantIndex(int[]) disassembly (after)
29   /// CHECK:                     BoundsCheck
30   /// CHECK:                     cmp {{r\d+}}, #0
31   /// CHECK:                     bls.w <<SLOW:0x[0-9a-f]+>>
32   /// CHECK:                     BoundsCheckSlowPathARMVIXL
33   /// CHECK-NEXT:                <<SLOW>>:
constantIndex(int[] a)34   public static int constantIndex(int[] a) {
35     try {
36       a[0] = 42;
37     } catch (ArrayIndexOutOfBoundsException expected) {
38       return -1;
39     }
40     return a.length;
41   }
42 
43   // Constant length, variable index.
44   /// CHECK-START-ARM64: int Main.constantLength(int) disassembly (after)
45   /// CHECK:                     BoundsCheck
46   /// CHECK:                     cmp {{w\d+}}, #0xa
47   /// CHECK:                     b.hs #+0x{{[0-9a-f]+}} (addr 0x<<SLOW:[0-9a-f]+>>)
48   /// CHECK:                     BoundsCheckSlowPathARM64
49   /// CHECK-NEXT:                0x<<SLOW>>:
50   /// CHECK-START-ARM: int Main.constantLength(int) disassembly (after)
51   /// CHECK:                     BoundsCheck
52   /// CHECK:                     cmp {{r\d+}}, #10
53   /// CHECK:                     bcs.w <<SLOW:0x[0-9a-f]+>>
54   /// CHECK:                     BoundsCheckSlowPathARMVIXL
55   /// CHECK-NEXT:                <<SLOW>>:
constantLength(int index)56   public static int constantLength(int index) {
57     int[] a = new int[10];
58     try {
59       a[index] = 1;
60     } catch (ArrayIndexOutOfBoundsException expected) {
61       return -1;
62     }
63     return index;
64   }
65 
66   // Constant index and length, out of bounds access. Check that we only have
67   // the slow path.
68   /// CHECK-START-ARM64: int Main.constantIndexAndLength() disassembly (after)
69   /// CHECK:                     BoundsCheck
70   /// CHECK-NOT:                 cmp
71   /// CHECK:                     b #+0x{{[0-9a-f]+}} (addr 0x<<SLOW:[0-9a-f]+>>)
72   /// CHECK:                     BoundsCheckSlowPathARM64
73   /// CHECK-NEXT:                0x<<SLOW>>:
74   /// CHECK-START-ARM: int Main.constantIndexAndLength() disassembly (after)
75   /// CHECK:                     BoundsCheck
76   /// CHECK-NOT:                 cmp
77   /// CHECK:                     b <<SLOW:0x[0-9a-f]+>>
78   /// CHECK:                     BoundsCheckSlowPathARMVIXL
79   /// CHECK-NEXT:                <<SLOW>>:
constantIndexAndLength()80   public static int constantIndexAndLength() {
81     try {
82       int[] a = new int[5];
83       a[10] = 42;
84     } catch (ArrayIndexOutOfBoundsException expected) {
85       return -1;
86     }
87     return 0;
88   }
89 
main(String[] args)90   public static void main(String[] args) {
91     int[] a = new int[10];
92     int[] b = new int[0];
93     expectEquals(a.length, constantIndex(a));
94     expectEquals(-1, constantIndex(b));
95     expectEquals(0, constantLength(0));
96     expectEquals(9, constantLength(9));
97     expectEquals(-1, constantLength(10));
98     expectEquals(-1, constantLength(-2));
99     expectEquals(-1, constantIndexAndLength());
100     System.out.println("passed");
101   }
102 
expectEquals(int expected, int result)103   private static void expectEquals(int expected, int result) {
104     if (expected != result) {
105       throw new Error("Expected: " + expected + ", found: " + result);
106     }
107   }
108 }
109