Throw ClassFormatError for unsupported default methods.

Fix DCHECK() failure for copied native default methods.
For dex file version 37+ we reject the dex file.
For older versions, throw the ClassFormatError like the RI.

Test: 180-native-default-method.
Test: m test-art-host-gtest
Test: testrunner.py --host --optimizing
Test: aosp_taimen-userdebug boots.
Bug: 157170505
Change-Id: I5d2aefdcbf0b807c1cb482d0fda2e77316783cb4
diff --git a/runtime/class_linker.cc b/runtime/class_linker.cc
index b6f417d..fa698f1 100644
--- a/runtime/class_linker.cc
+++ b/runtime/class_linker.cc
@@ -6430,6 +6430,15 @@
       ArtMethod* m = klass->GetVirtualMethodDuringLinking(i, image_pointer_size_);
       m->SetMethodIndex(i);
       if (!m->IsAbstract()) {
+        // If the dex file does not support default methods, throw ClassFormatError.
+        // This check is necessary to protect from odd cases, such as native default
+        // methods, that the dex file verifier permits for old dex file versions. b/157170505
+        if (!m->GetDexFile()->SupportsDefaultMethods()) {
+          ThrowClassFormatError(klass.Get(),
+                                "Dex file does not support default method '%s'",
+                                m->PrettyMethod().c_str());
+          return false;
+        }
         m->SetAccessFlags(m->GetAccessFlags() | kAccDefault);
         has_defaults = true;
       }
diff --git a/test/180-native-default-method/build b/test/180-native-default-method/build
new file mode 100644
index 0000000..3963fd3
--- /dev/null
+++ b/test/180-native-default-method/build
@@ -0,0 +1,30 @@
+#!/bin/bash
+#
+# Copyright 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.
+
+# make us exit on a failure
+set -e
+
+./default-build "$@"
+
+if [[ $@ != *"--jvm"* ]]; then
+  # Change the generated dex file to have a v35 magic number if it is version 39
+  if test -f classes.dex && head -c 7 classes.dex | grep -q 039; then
+    # place ascii value '035' into the classes.dex file starting at byte 4.
+    printf '035' | dd status=none conv=notrunc of=classes.dex bs=1 seek=4 count=3
+    rm -f $TEST_NAME.jar
+    zip $TEST_NAME.jar classes.dex
+  fi
+fi
diff --git a/test/180-native-default-method/expected.txt b/test/180-native-default-method/expected.txt
new file mode 100644
index 0000000..b0aad4d
--- /dev/null
+++ b/test/180-native-default-method/expected.txt
@@ -0,0 +1 @@
+passed
diff --git a/test/180-native-default-method/info.txt b/test/180-native-default-method/info.txt
new file mode 100644
index 0000000..0cba4eb
--- /dev/null
+++ b/test/180-native-default-method/info.txt
@@ -0,0 +1,3 @@
+Regression test for DCHECK() failure for copying a default native method from
+an interface to a class implementing that interface. The default native method
+should result in ClassFormatError before we reach that DCHECK().
diff --git a/test/180-native-default-method/jasmin/TestClass.j b/test/180-native-default-method/jasmin/TestClass.j
new file mode 100644
index 0000000..fddd99b
--- /dev/null
+++ b/test/180-native-default-method/jasmin/TestClass.j
@@ -0,0 +1,25 @@
+; 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 TestClass
+.super                   java/lang/Object
+.implements              TestInterface
+
+.method                  public <init>()V
+   .limit stack          1
+   .limit locals         1
+   aload_0
+   invokespecial         java/lang/Object/<init>()V
+   return
+.end method
diff --git a/test/180-native-default-method/jasmin/TestInterface.j b/test/180-native-default-method/jasmin/TestInterface.j
new file mode 100644
index 0000000..080474e
--- /dev/null
+++ b/test/180-native-default-method/jasmin/TestInterface.j
@@ -0,0 +1,19 @@
+; 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.
+
+.interface               public TestInterface
+.super                   java/lang/Object
+
+.method                  public native foo()V
+.end method
diff --git a/test/180-native-default-method/src/Main.java b/test/180-native-default-method/src/Main.java
new file mode 100644
index 0000000..4b2704b
--- /dev/null
+++ b/test/180-native-default-method/src/Main.java
@@ -0,0 +1,32 @@
+/*
+ * 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 main(String args[]) {
+    try {
+      // Regression test for default native methods that should cause ClassFormatException
+      // if they pass the dex file verification, i.e. for old dex file versions.
+      // We previously did not handle this case properly and failed a DCHECK() for
+      // a non-interface class creating a copied method that was native. b/157170505
+      Class.forName("TestClass");
+      throw new Error("UNREACHABLE");
+    } catch (ClassFormatError expected) {
+      System.out.println("passed");
+    } catch (Throwable unexpected) {
+      unexpected.printStackTrace();
+    }
+  }
+}