1f0e3937b87453234d0d7970b8712082062709b8dNicolas Geoffray/*
2f0e3937b87453234d0d7970b8712082062709b8dNicolas Geoffray * Copyright (C) 2014 The Android Open Source Project
3f0e3937b87453234d0d7970b8712082062709b8dNicolas Geoffray *
4f0e3937b87453234d0d7970b8712082062709b8dNicolas Geoffray * Licensed under the Apache License, Version 2.0 (the "License");
5f0e3937b87453234d0d7970b8712082062709b8dNicolas Geoffray * you may not use this file except in compliance with the License.
6f0e3937b87453234d0d7970b8712082062709b8dNicolas Geoffray * You may obtain a copy of the License at
7f0e3937b87453234d0d7970b8712082062709b8dNicolas Geoffray *
8f0e3937b87453234d0d7970b8712082062709b8dNicolas Geoffray *      http://www.apache.org/licenses/LICENSE-2.0
9f0e3937b87453234d0d7970b8712082062709b8dNicolas Geoffray *
10f0e3937b87453234d0d7970b8712082062709b8dNicolas Geoffray * Unless required by applicable law or agreed to in writing, software
11f0e3937b87453234d0d7970b8712082062709b8dNicolas Geoffray * distributed under the License is distributed on an "AS IS" BASIS,
12f0e3937b87453234d0d7970b8712082062709b8dNicolas Geoffray * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13f0e3937b87453234d0d7970b8712082062709b8dNicolas Geoffray * See the License for the specific language governing permissions and
14f0e3937b87453234d0d7970b8712082062709b8dNicolas Geoffray * limitations under the License.
15f0e3937b87453234d0d7970b8712082062709b8dNicolas Geoffray */
16f0e3937b87453234d0d7970b8712082062709b8dNicolas Geoffray
17f0e3937b87453234d0d7970b8712082062709b8dNicolas Geoffraypublic class Main {
18f0e3937b87453234d0d7970b8712082062709b8dNicolas Geoffray  public static void main(String[] args) {
19f0e3937b87453234d0d7970b8712082062709b8dNicolas Geoffray    Exception exception = null;
20f0e3937b87453234d0d7970b8712082062709b8dNicolas Geoffray    try {
21f0e3937b87453234d0d7970b8712082062709b8dNicolas Geoffray      $opt$Throw(new int[1]);
22f0e3937b87453234d0d7970b8712082062709b8dNicolas Geoffray    } catch (ArrayIndexOutOfBoundsException e) {
23f0e3937b87453234d0d7970b8712082062709b8dNicolas Geoffray      exception = e;
24f0e3937b87453234d0d7970b8712082062709b8dNicolas Geoffray    }
25f0e3937b87453234d0d7970b8712082062709b8dNicolas Geoffray
26f0e3937b87453234d0d7970b8712082062709b8dNicolas Geoffray    String exceptionMessage = exception.getMessage();
27f0e3937b87453234d0d7970b8712082062709b8dNicolas Geoffray
28f0e3937b87453234d0d7970b8712082062709b8dNicolas Geoffray    // Note that it's ART specific to emit the length.
29f0e3937b87453234d0d7970b8712082062709b8dNicolas Geoffray    if (exceptionMessage.contains("length")) {
30f0e3937b87453234d0d7970b8712082062709b8dNicolas Geoffray      if (!exceptionMessage.contains("length=1")) {
31f0e3937b87453234d0d7970b8712082062709b8dNicolas Geoffray        throw new Error("Wrong length in exception message");
32f0e3937b87453234d0d7970b8712082062709b8dNicolas Geoffray      }
33f0e3937b87453234d0d7970b8712082062709b8dNicolas Geoffray    }
34f0e3937b87453234d0d7970b8712082062709b8dNicolas Geoffray
35f0e3937b87453234d0d7970b8712082062709b8dNicolas Geoffray    // Note that it's ART specific to emit the index.
36f0e3937b87453234d0d7970b8712082062709b8dNicolas Geoffray    if (exceptionMessage.contains("index")) {
37f0e3937b87453234d0d7970b8712082062709b8dNicolas Geoffray      if (!exceptionMessage.contains("index=2")) {
38f0e3937b87453234d0d7970b8712082062709b8dNicolas Geoffray        throw new Error("Wrong index in exception message");
39f0e3937b87453234d0d7970b8712082062709b8dNicolas Geoffray      }
40f0e3937b87453234d0d7970b8712082062709b8dNicolas Geoffray    }
41f0e3937b87453234d0d7970b8712082062709b8dNicolas Geoffray  }
42f0e3937b87453234d0d7970b8712082062709b8dNicolas Geoffray
43f0e3937b87453234d0d7970b8712082062709b8dNicolas Geoffray  static void $opt$Throw(int[] array) {
44f0e3937b87453234d0d7970b8712082062709b8dNicolas Geoffray    // We fetch the length first, to ensure it is in EAX (on x86).
45f0e3937b87453234d0d7970b8712082062709b8dNicolas Geoffray    // The pThrowArrayBounds entrypoint expects the index in EAX and the
46f0e3937b87453234d0d7970b8712082062709b8dNicolas Geoffray    // length in ECX, and the optimizing compiler used to write to EAX
47f0e3937b87453234d0d7970b8712082062709b8dNicolas Geoffray    // before putting the length in ECX.
48f0e3937b87453234d0d7970b8712082062709b8dNicolas Geoffray    int length = array.length;
49f0e3937b87453234d0d7970b8712082062709b8dNicolas Geoffray    array[2] = 42;
50f0e3937b87453234d0d7970b8712082062709b8dNicolas Geoffray  }
51f0e3937b87453234d0d7970b8712082062709b8dNicolas Geoffray}
52