Fix incorrect interface-override linking

We would incorrectly search all declared methods when searching for
overrides of super-class interface methods. This could lead to
incorrect behavior since we could try to override methods with static
or private methods. This could cause exceptions or other issues.

This fixes a typo from over 4 years ago in go/aog/185608 and restores
the behavior we had prior to N.

Bug: 152199517
Test: ./test.py --host
Change-Id: Ic6ce51f79d071727316bb39e5bbeabb92cb4aa9a
diff --git a/runtime/class_linker.cc b/runtime/class_linker.cc
index 3376bdd..27bdfe6 100644
--- a/runtime/class_linker.cc
+++ b/runtime/class_linker.cc
@@ -8155,7 +8155,7 @@
         // If we are overwriting a super class interface, try to only virtual methods instead of the
         // whole vtable.
         using_virtuals = true;
-        input_virtual_methods = klass->GetDeclaredMethodsSlice(image_pointer_size_);
+        input_virtual_methods = klass->GetDeclaredVirtualMethodsSlice(image_pointer_size_);
         input_array_length = input_virtual_methods.size();
       } else {
         // For a new interface, however, we need the whole vtable in case a new
@@ -8191,6 +8191,7 @@
               input_vtable_array->GetElementPtrSize<ArtMethod*>(k, image_pointer_size_);
           ArtMethod* vtable_method_for_name_comparison =
               vtable_method->GetInterfaceMethodIfProxy(image_pointer_size_);
+          DCHECK(!vtable_method->IsStatic()) << vtable_method->PrettyMethod();
           if (interface_name_comparator.HasSameNameAndSignature(
               vtable_method_for_name_comparison)) {
             if (!vtable_method->IsAbstract() && !vtable_method->IsPublic()) {
diff --git a/test/2032-default-method-private-override/expected.txt b/test/2032-default-method-private-override/expected.txt
new file mode 100644
index 0000000..349ae32
--- /dev/null
+++ b/test/2032-default-method-private-override/expected.txt
@@ -0,0 +1,6 @@
+Concrete1
+Hello
+Concrete2
+Hello
+Concrete3
+Hello
diff --git a/test/2032-default-method-private-override/info.txt b/test/2032-default-method-private-override/info.txt
new file mode 100644
index 0000000..8367a3d
--- /dev/null
+++ b/test/2032-default-method-private-override/info.txt
@@ -0,0 +1,7 @@
+Regression test for b/152199517
+
+We would incorrectly search all declared methods of a class for interface
+implementations instead of restricting ourselves to virtual methods when
+looking for overrides to a superclasses interfaces. This could cause
+exceptions and incorrect behavior as we might try to use a private or a
+static method as an interface implementation.
diff --git a/test/2032-default-method-private-override/jasmin/Concrete1.j b/test/2032-default-method-private-override/jasmin/Concrete1.j
new file mode 100644
index 0000000..fbf62eb
--- /dev/null
+++ b/test/2032-default-method-private-override/jasmin/Concrete1.j
@@ -0,0 +1,34 @@
+; Copyright (C) 2020 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 Concrete1
+.super java/lang/Object
+.implements IFace
+
+.method public <init>()V
+  .limit stack 1
+  .limit locals 1
+  aload_0
+  invokespecial java/lang/Object/<init>()V
+  return
+.end method
+
+.method private static sayHi()V
+  .limit stack 2
+  .limit locals 2
+  getstatic java/lang/System/out Ljava/io/PrintStream;
+  ldc "Hello from a private method!"
+  invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V
+  return
+.end method
diff --git a/test/2032-default-method-private-override/jasmin/Concrete2.j b/test/2032-default-method-private-override/jasmin/Concrete2.j
new file mode 100644
index 0000000..00eee98
--- /dev/null
+++ b/test/2032-default-method-private-override/jasmin/Concrete2.j
@@ -0,0 +1,34 @@
+; Copyright (C) 2020 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 Concrete2
+; Our superclass implements the IFace interface.
+.super Concrete2Base
+
+.method public <init>()V
+  .limit stack 1
+  .limit locals 1
+  aload_0
+  invokespecial Concrete2Base/<init>()V
+  return
+.end method
+
+.method private static sayHi()V
+  .limit stack 2
+  .limit locals 2
+  getstatic java/lang/System/out Ljava/io/PrintStream;
+  ldc "Hello from a private method!"
+  invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V
+  return
+.end method
diff --git a/test/2032-default-method-private-override/jasmin/Concrete3.j b/test/2032-default-method-private-override/jasmin/Concrete3.j
new file mode 100644
index 0000000..cb231e8
--- /dev/null
+++ b/test/2032-default-method-private-override/jasmin/Concrete3.j
@@ -0,0 +1,34 @@
+; Copyright (C) 2020 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 Concrete3
+; Our superclass implements the IFace interface.
+.super Concrete2Base
+
+.method public <init>()V
+  .limit stack 1
+  .limit locals 1
+  aload_0
+  invokespecial Concrete2Base/<init>()V
+  return
+.end method
+
+.method public static sayHi()V
+  .limit stack 2
+  .limit locals 2
+  getstatic java/lang/System/out Ljava/io/PrintStream;
+  ldc "Hello from a private method!"
+  invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V
+  return
+.end method
diff --git a/test/2032-default-method-private-override/src/Concrete2Base.java b/test/2032-default-method-private-override/src/Concrete2Base.java
new file mode 100644
index 0000000..470e844
--- /dev/null
+++ b/test/2032-default-method-private-override/src/Concrete2Base.java
@@ -0,0 +1,17 @@
+/*
+ * Copyright (C) 2020 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 Concrete2Base implements IFace { }
diff --git a/test/2032-default-method-private-override/src/IFace.java b/test/2032-default-method-private-override/src/IFace.java
new file mode 100644
index 0000000..2cec4f1
--- /dev/null
+++ b/test/2032-default-method-private-override/src/IFace.java
@@ -0,0 +1,21 @@
+/*
+ * Copyright (C) 2020 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 interface IFace {
+  public default void sayHi() {
+    System.out.println("Hello");
+  }
+}
diff --git a/test/2032-default-method-private-override/src/Main.java b/test/2032-default-method-private-override/src/Main.java
new file mode 100644
index 0000000..4e17a21
--- /dev/null
+++ b/test/2032-default-method-private-override/src/Main.java
@@ -0,0 +1,33 @@
+/*
+ * Copyright (C) 2020 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 Main {
+  public static void sayHi(String klass) throws Exception {
+    try {
+      System.out.println(klass);
+      IFace iface = (IFace)Class.forName(klass).newInstance();
+      iface.sayHi();
+    } catch (Exception e) {
+      System.out.println("Exception thrown!");
+      System.out.println(e);
+    }
+  }
+  public static void main(String[] args) throws Exception {
+    sayHi("Concrete1");
+    sayHi("Concrete2");
+    sayHi("Concrete3");
+  }
+}