Fix StackWalk test.

- Make sure methods expected in the C code do exist.
- Adjust new register expectations for $noinline$f
- Make sure all methods were compiled.
- Remove unused test/StackWalk2/StackWalk2.java

Test: 004-StackWalk
Change-Id: I0bd2fa63a152dc696ea0516a5c3f4a2008e7ef2c
diff --git a/test/004-StackWalk/build b/test/004-StackWalk/build
index eeecbfc..3de541c 100644
--- a/test/004-StackWalk/build
+++ b/test/004-StackWalk/build
@@ -18,10 +18,8 @@
 set -e
 
 # This test depends on the exact format of the DEX file. Since dx is deprecated,
-# the classes.dex file is packaged as a test input. It was created with:
-#
-# $ javac -g -Xlint:-options -source 1.7 -target 1.7 -d classes src/Main.java
-# $ dx --debug --dex --output=classes.dex classes
+# the classes.dex file is packaged as a test input. See src/Main.java file
+# to check how it was created.
 
 # Wrapper function for javac which for this test does nothing as the
 # test uses a pre-built DEX file.
diff --git a/test/004-StackWalk/classes.dex b/test/004-StackWalk/classes.dex
index ad45296..61a7277 100644
--- a/test/004-StackWalk/classes.dex
+++ b/test/004-StackWalk/classes.dex
Binary files differ
diff --git a/test/004-StackWalk/src/Main.java b/test/004-StackWalk/src/Main.java
index 072b1d0..2a098f7 100644
--- a/test/004-StackWalk/src/Main.java
+++ b/test/004-StackWalk/src/Main.java
@@ -1,19 +1,36 @@
+/*
+ * Copyright (C) 2011 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.
+ */
+
+// This test depends on the exact format of the DEX file. Since dx is deprecated,
+// the classes.dex file is packaged as a test input. It was created with:
+//
+// $ javac -g -Xlint:-options -source 1.7 -target 1.7 -d classes src/Main.java
+// $ dx --debug --dex --output=classes.dex classes
+
 public class Main {
   public Main() {
   }
 
-  boolean doThrow = false;
-
   int $noinline$f() throws Exception {
-    g(1);
-    g(2);
-
-    // This currently defeats inlining of `f`.
-    if (doThrow) { throw new Error(); }
+    $noinline$g(1);
+    $noinline$g(2);
     return 0;
   }
 
-  void g(int num_calls) {
+  void $noinline$g(int num_calls) {
     if (num_calls == 1) {
       System.out.println("1st call");
     } else if (num_calls == 2) {
@@ -81,11 +98,14 @@
     s4 = s18 = s19;
     s += s4;
     s += s18;
-    stackmap(0);
-    return s;
+    // Add a branch to workaround ART's large methods without branches heuristic.
+    if (testStackWalk(0) != 0) {
+      return s;
+    }
+    return s18;
   }
 
-  native int stackmap(int x);
+  native int testStackWalk(int x);
 
   public static void main(String[] args) throws Exception {
     System.loadLibrary(args[0]);
diff --git a/test/004-StackWalk/stack_walk_jni.cc b/test/004-StackWalk/stack_walk_jni.cc
index 89e2e66..53e0dae 100644
--- a/test/004-StackWalk/stack_walk_jni.cc
+++ b/test/004-StackWalk/stack_walk_jni.cc
@@ -20,7 +20,7 @@
 
 namespace art {
 
-#define CHECK_REGS(...) do { \
+#define CHECK_REGS_ARE_REFERENCES(...) do { \
   int t[] = {__VA_ARGS__}; \
   int t_size = sizeof(t) / sizeof(*t); \
   CheckReferences(t, t_size, GetNativePcOffset()); \
@@ -43,40 +43,50 @@
     // Given the method name and the number of times the method has been called,
     // we know the Dex registers with live reference values. Assert that what we
     // find is what is expected.
-    if (m_name == "f") {
+    if (m_name == "$noinline$f") {
       if (gJava_StackWalk_refmap_calls == 1) {
         CHECK_EQ(1U, GetDexPc());
-        CHECK_REGS(4);
+        CHECK_REGS_ARE_REFERENCES(1);
       } else {
         CHECK_EQ(gJava_StackWalk_refmap_calls, 2);
         CHECK_EQ(5U, GetDexPc());
-        CHECK_REGS(4);
+        CHECK_REGS_ARE_REFERENCES(1);
       }
-    } else if (m_name == "g") {
+      found_f_ = true;
+    } else if (m_name == "$noinline$g") {
       if (gJava_StackWalk_refmap_calls == 1) {
         CHECK_EQ(0xcU, GetDexPc());
-        CHECK_REGS(0, 2);  // Note that v1 is not in the minimal root set
+        CHECK_REGS_ARE_REFERENCES(0, 2);  // Note that v1 is not in the minimal root set
       } else {
         CHECK_EQ(gJava_StackWalk_refmap_calls, 2);
         CHECK_EQ(0xcU, GetDexPc());
-        CHECK_REGS(0, 2);
+        CHECK_REGS_ARE_REFERENCES(0, 2);
       }
+      found_g_ = true;
     } else if (m_name == "shlemiel") {
       if (gJava_StackWalk_refmap_calls == 1) {
         CHECK_EQ(0x380U, GetDexPc());
-        CHECK_REGS(2, 4, 5, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 19, 21, 25);
+        CHECK_REGS_ARE_REFERENCES(2, 4, 5, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 19, 21, 25);
       } else {
         CHECK_EQ(gJava_StackWalk_refmap_calls, 2);
         CHECK_EQ(0x380U, GetDexPc());
-        CHECK_REGS(2, 4, 5, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 19, 21, 25);
+        CHECK_REGS_ARE_REFERENCES(2, 4, 5, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 19, 21, 25);
       }
+      found_shlemiel_ = true;
     }
 
     return true;
   }
+
+  ~TestReferenceMapVisitor() {
+  }
+
+  bool found_f_ = false;
+  bool found_g_ = false;
+  bool found_shlemiel_ = false;
 };
 
-extern "C" JNIEXPORT jint JNICALL Java_Main_stackmap(JNIEnv*, jobject, jint count) {
+extern "C" JNIEXPORT jint JNICALL Java_Main_testStackWalk(JNIEnv*, jobject, jint count) {
   ScopedObjectAccess soa(Thread::Current());
   CHECK_EQ(count, 0);
   gJava_StackWalk_refmap_calls++;
@@ -84,17 +94,9 @@
   // Visitor
   TestReferenceMapVisitor mapper(soa.Self());
   mapper.WalkStack();
-
-  return count + 1;
-}
-
-extern "C" JNIEXPORT jint JNICALL Java_Main_refmap2(JNIEnv*, jobject, jint count) {
-  ScopedObjectAccess soa(Thread::Current());
-  gJava_StackWalk_refmap_calls++;
-
-  // Visitor
-  TestReferenceMapVisitor mapper(soa.Self());
-  mapper.WalkStack();
+  CHECK(mapper.found_f_);
+  CHECK(mapper.found_g_);
+  CHECK(mapper.found_shlemiel_);
 
   return count + 1;
 }
diff --git a/test/StackWalk2/StackWalk2.java b/test/StackWalk2/StackWalk2.java
deleted file mode 100644
index 5e7b22c..0000000
--- a/test/StackWalk2/StackWalk2.java
+++ /dev/null
@@ -1,58 +0,0 @@
-/*
- * Copyright (C) 2011 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.
- */
-
-public class StackWalk2 {
-  // use v1 for this
-
-  String str = new String();  // use v0 for str in <init>
-
-  int f() {
-    g(1);  // use v0 for 1, v1 for this
-    g(2);  // use v0 for 2, v1 for this
-    strTest();  // use v1 for this
-    return 0;
-  }
-
-  void g(int num_calls) throws RuntimeException {
-    if (num_calls == 1) {  // use v0 for 1, v3 for num_calls
-      System.logI("1st call");  // use v0 for PrintStream, v1 for "1st call"
-      refmap2(24);  // use v0 for 24, v2 for this
-    } else if (num_calls == 2) {  // use v0 for 2, v3 for num_calls
-      System.logI("2nd call");  // use v0 for PrintStream, v1 for "2nd call"
-      refmap2(25);  // use v0 for 24, v2 for this
-    }
-    throw new RuntimeException();  // use v0 for new RuntimeException
-  }
-
-  void strTest() {
-    System.logI(str);  // use v1 for PrintStream, v2, v3 for str
-    str = null;  // use v1 for null, v3 for str
-    str = new String("ya");  // use v2 for "ya", v1 for new String
-    String s = str;  // use v0, v1, v3
-    System.logI(str);  // use v1 for PrintStream, v2, v3 for str
-    System.logI(s);  // use v1 for PrintStream, v0 for s
-    s = null;  // use v0
-    System.logI(s);  // use v1 for PrintStream, v0 for s
-  }
-
-  native int refmap2(int x);
-
-  public static void main(String[] args) {
-    System.loadLibrary(args[0]);
-    StackWalk2 st = new StackWalk2();
-    st.f();
-  }
-}
diff --git a/test/knownfailures.json b/test/knownfailures.json
index 6004f25..05c9aa9 100644
--- a/test/knownfailures.json
+++ b/test/knownfailures.json
@@ -697,9 +697,9 @@
         "description": ["Tests that depend on input-vdex are not supported with compact dex"]
     },
     {
-        "tests": "661-oat-writer-layout",
+        "tests": ["661-oat-writer-layout", "004-StackWalk"],
         "variant": "interp-ac | interpreter | jit | no-prebuild | no-image | trace | redefine-stress | jvmti-stress",
-        "description": ["Test is designed to only check --compiler-filter=speed"]
+        "description": ["Tests are designed to only check --optimizing"]
     },
     {
         "tests": "674-HelloWorld-Dm",