Merge "Clear shadow registers only after potentially switching interpreters."
diff --git a/runtime/interpreter/mterp/arm/op_aget_wide.S b/runtime/interpreter/mterp/arm/op_aget_wide.S
index 853a7a4..66ec950 100644
--- a/runtime/interpreter/mterp/arm/op_aget_wide.S
+++ b/runtime/interpreter/mterp/arm/op_aget_wide.S
@@ -10,7 +10,6 @@
     mov     r3, r0, lsr #8              @ r3<- CC
     GET_VREG r0, r2                     @ r0<- vBB (array object)
     GET_VREG r1, r3                     @ r1<- vCC (requested index)
-    CLEAR_SHADOW_PAIR r9, r2, r3        @ Zero out the shadow regs
     cmp     r0, #0                      @ null array object?
     beq     common_errNullObject        @ yes, bail
     ldr     r3, [r0, #MIRROR_ARRAY_LENGTH_OFFSET]    @ r3<- arrayObj->length
@@ -18,6 +17,7 @@
     cmp     r1, r3                      @ compare unsigned index, length
     bcs     common_errArrayIndex        @ index >= length, bail
     FETCH_ADVANCE_INST 2                @ advance rPC, load rINST
+    CLEAR_SHADOW_PAIR r9, lr, ip        @ Zero out the shadow regs
     ldrd    r2, [r0, #MIRROR_WIDE_ARRAY_DATA_OFFSET]  @ r2/r3<- vBB[vCC]
     VREG_INDEX_TO_ADDR r9, r9           @ r9<- &fp[AA]
     GET_INST_OPCODE ip                  @ extract opcode from rINST
diff --git a/runtime/interpreter/mterp/out/mterp_arm.S b/runtime/interpreter/mterp/out/mterp_arm.S
index 8ca5bd4..be16e7d 100644
--- a/runtime/interpreter/mterp/out/mterp_arm.S
+++ b/runtime/interpreter/mterp/out/mterp_arm.S
@@ -1836,7 +1836,6 @@
     mov     r3, r0, lsr #8              @ r3<- CC
     GET_VREG r0, r2                     @ r0<- vBB (array object)
     GET_VREG r1, r3                     @ r1<- vCC (requested index)
-    CLEAR_SHADOW_PAIR r9, r2, r3        @ Zero out the shadow regs
     cmp     r0, #0                      @ null array object?
     beq     common_errNullObject        @ yes, bail
     ldr     r3, [r0, #MIRROR_ARRAY_LENGTH_OFFSET]    @ r3<- arrayObj->length
@@ -1844,6 +1843,7 @@
     cmp     r1, r3                      @ compare unsigned index, length
     bcs     common_errArrayIndex        @ index >= length, bail
     FETCH_ADVANCE_INST 2                @ advance rPC, load rINST
+    CLEAR_SHADOW_PAIR r9, lr, ip        @ Zero out the shadow regs
     ldrd    r2, [r0, #MIRROR_WIDE_ARRAY_DATA_OFFSET]  @ r2/r3<- vBB[vCC]
     VREG_INDEX_TO_ADDR r9, r9           @ r9<- &fp[AA]
     GET_INST_OPCODE ip                  @ extract opcode from rINST
diff --git a/test/668-aiobe/expected.txt b/test/668-aiobe/expected.txt
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/test/668-aiobe/expected.txt
diff --git a/test/668-aiobe/info.txt b/test/668-aiobe/info.txt
new file mode 100644
index 0000000..e422601
--- /dev/null
+++ b/test/668-aiobe/info.txt
@@ -0,0 +1,2 @@
+Regression test for the mterp arm interpreter which used to throw
+the wrong exception when accessing out of bounds a long/double array.
diff --git a/test/668-aiobe/smali/TestCase.smali b/test/668-aiobe/smali/TestCase.smali
new file mode 100644
index 0000000..5fa62e9
--- /dev/null
+++ b/test/668-aiobe/smali/TestCase.smali
@@ -0,0 +1,30 @@
+#
+# Copyright (C) 2017 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+
+.class public LTestCase;
+.super Ljava/lang/Object;
+
+.method public constructor <init>()V
+.registers 1
+    invoke-direct {v0}, Ljava/lang/Object;-><init>()V
+    return-void
+.end method
+
+.method public static run([DI)D
+.registers 2
+    aget-wide p0, p0, p1
+    return-wide p0
+.end method
diff --git a/test/668-aiobe/src/Main.java b/test/668-aiobe/src/Main.java
new file mode 100644
index 0000000..2bd30c4
--- /dev/null
+++ b/test/668-aiobe/src/Main.java
@@ -0,0 +1,36 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+public class Main {
+  public static void main(String[] args) throws Exception {
+    double[] array = new double[5];
+    try {
+      Class<?> c = Class.forName("TestCase");
+      Method m = c.getMethod("run", double[].class, int.class);
+      m.invoke(null, array, 42);
+    } catch (InvocationTargetException e) {
+      // expected
+      if (!(e.getCause() instanceof ArrayIndexOutOfBoundsException)) {
+        throw new Error("Expected ArrayIndexOutOfBoundsException, got " + e.getCause());
+      }
+      return;
+    }
+    throw new Error("Expected InvocationTargetException");
+  }
+}