Fix missing suspend check in nterp for goto32 +0
Test: 830-goto-zero
Bug: 200660605
Change-Id: I2267fa3d2842cc84e2e5b3ee8cf04989cd423a50
diff --git a/runtime/interpreter/mterp/arm64ng/main.S b/runtime/interpreter/mterp/arm64ng/main.S
index 627bb04..79eca04 100644
--- a/runtime/interpreter/mterp/arm64ng/main.S
+++ b/runtime/interpreter/mterp/arm64ng/main.S
@@ -290,8 +290,9 @@
.endm
.macro BRANCH
- // Update method counter and do a suspend check if the branch is negative.
- tbnz wINST, #31, 2f
+ // Update method counter and do a suspend check if the branch is negative or zero.
+ cmp wINST, #0
+ b.le 2f
1:
add xPC, xPC, wINST, sxtw #1 // update xPC
FETCH wINST, 0 // load wINST
diff --git a/runtime/interpreter/mterp/armng/main.S b/runtime/interpreter/mterp/armng/main.S
index 4427908..932f6ac 100644
--- a/runtime/interpreter/mterp/armng/main.S
+++ b/runtime/interpreter/mterp/armng/main.S
@@ -301,9 +301,9 @@
.endm
.macro BRANCH
- // Update method counter and do a suspend check if the branch is negative.
+ // Update method counter and do a suspend check if the branch is negative or zero.
cmp rINST, #0
- blt 2f
+ ble 2f
1:
add r2, rINST, rINST // r2<- byte offset
FETCH_ADVANCE_INST_RB r2 // update xPC, load rINST
diff --git a/runtime/interpreter/mterp/x86_64ng/main.S b/runtime/interpreter/mterp/x86_64ng/main.S
index 71df9e1..33912d7 100644
--- a/runtime/interpreter/mterp/x86_64ng/main.S
+++ b/runtime/interpreter/mterp/x86_64ng/main.S
@@ -251,9 +251,9 @@
.macro BRANCH
- // Update method counter and do a suspend check if the branch is negative.
+ // Update method counter and do a suspend check if the branch is negative or zero.
testq rINSTq, rINSTq
- js 3f
+ jle 3f
2:
leaq (rPC, rINSTq, 2), rPC
FETCH_INST
diff --git a/runtime/interpreter/mterp/x86ng/main.S b/runtime/interpreter/mterp/x86ng/main.S
index 6ee9193..7f185b0 100644
--- a/runtime/interpreter/mterp/x86ng/main.S
+++ b/runtime/interpreter/mterp/x86ng/main.S
@@ -293,9 +293,9 @@
.macro BRANCH
- // Update method counter and do a suspend check if the branch is negative.
+ // Update method counter and do a suspend check if the branch is negative or zero.
testl rINST, rINST
- js 3f
+ jle 3f
2:
leal (rPC, rINST, 2), rPC
FETCH_INST
diff --git a/test/830-goto-zero/expected-stderr.txt b/test/830-goto-zero/expected-stderr.txt
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/test/830-goto-zero/expected-stderr.txt
diff --git a/test/830-goto-zero/expected-stdout.txt b/test/830-goto-zero/expected-stdout.txt
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/test/830-goto-zero/expected-stdout.txt
diff --git a/test/830-goto-zero/info.txt b/test/830-goto-zero/info.txt
new file mode 100644
index 0000000..356d22f
--- /dev/null
+++ b/test/830-goto-zero/info.txt
@@ -0,0 +1 @@
+Regression test for missing suspend checks in nterp when branching to zero.
diff --git a/test/830-goto-zero/smali/SmaliClass.smali b/test/830-goto-zero/smali/SmaliClass.smali
new file mode 100644
index 0000000..06ad3d4
--- /dev/null
+++ b/test/830-goto-zero/smali/SmaliClass.smali
@@ -0,0 +1,29 @@
+# Copyright (C) 2021 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 LSmaliClass;
+.super Ljava/lang/Object;
+
+.method public constructor <init>()V
+ .registers 1
+ invoke-direct {p0}, Ljava/lang/Object;-><init>()V
+ return-void
+.end method
+
+.method public static gotoZero()V
+ .registers 0
+ :Linfinite_loop
+ goto/32 :Linfinite_loop
+ return-void
+.end method
diff --git a/test/830-goto-zero/src/Main.java b/test/830-goto-zero/src/Main.java
new file mode 100644
index 0000000..d3d0d2c
--- /dev/null
+++ b/test/830-goto-zero/src/Main.java
@@ -0,0 +1,43 @@
+/*
+ * Copyright (C) 2021 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.Method;
+
+public class Main {
+
+ public static void main(String args[]) throws Exception {
+ b2302318Test();
+ }
+
+ static void b2302318Test() {
+ SpinThread st = new SpinThread();
+ st.setDaemon(true);
+ st.start();
+ Thread.yield();
+ Runtime.getRuntime().gc();
+ }
+
+}
+class SpinThread extends Thread {
+ public void run() {
+ try {
+ Class<?> cls = Class.forName("SmaliClass");
+ cls.getDeclaredMethod("gotoZero").invoke(null);
+ } catch (Exception e) {
+ throw new Error(e);
+ }
+ }
+}
diff --git a/test/knownfailures.json b/test/knownfailures.json
index 01a7f3b..69e3806 100644
--- a/test/knownfailures.json
+++ b/test/knownfailures.json
@@ -1155,6 +1155,7 @@
"821-many-args",
"822-hiddenapi-future",
"827-resolve-method",
+ "830-goto-zero",
"999-redefine-hiddenapi",
"1000-non-moving-space-stress",
"1001-app-image-regions",