ART: Fix missing field type crasher

Avoids nullptr dereference when a field with a missing field type is
assigned.

Bug: b/79751666
Test: art/test.py --host -r -t 173
Change-Id: I5868793f57802ed3de60264eefa7eb98845ae126
diff --git a/runtime/common_dex_operations.h b/runtime/common_dex_operations.h
index 9c2a40b..c29043e 100644
--- a/runtime/common_dex_operations.h
+++ b/runtime/common_dex_operations.h
@@ -205,7 +205,12 @@
           HandleWrapperObjPtr<mirror::Object> h_obj(hs.NewHandleWrapper(&obj));
           field_class = field->ResolveType();
         }
-        if (!reg->VerifierInstanceOf(field_class.Ptr())) {
+        // ArtField::ResolveType() may fail as evidenced with a dexing bug (b/78788577).
+        if (UNLIKELY(field_class.IsNull())) {
+          Thread::Current()->AssertPendingException();
+          return false;
+        }
+        if (UNLIKELY(!reg->VerifierInstanceOf(field_class.Ptr()))) {
           // This should never happen.
           std::string temp1, temp2, temp3;
           self->ThrowNewExceptionF("Ljava/lang/InternalError;",
diff --git a/test/173-missing-field-type/expected.txt b/test/173-missing-field-type/expected.txt
new file mode 100644
index 0000000..b0aad4d
--- /dev/null
+++ b/test/173-missing-field-type/expected.txt
@@ -0,0 +1 @@
+passed
diff --git a/test/173-missing-field-type/info.txt b/test/173-missing-field-type/info.txt
new file mode 100644
index 0000000..4e20b4c
--- /dev/null
+++ b/test/173-missing-field-type/info.txt
@@ -0,0 +1 @@
+Tests handling of fields where the field type is missing (b/79751666).
diff --git a/test/173-missing-field-type/smali/BadField.smali b/test/173-missing-field-type/smali/BadField.smali
new file mode 100644
index 0000000..851e8fe
--- /dev/null
+++ b/test/173-missing-field-type/smali/BadField.smali
@@ -0,0 +1,34 @@
+#
+# Copyright (C) 2018 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 LBadField;
+.super Ljava/lang/Object;
+
+# This is a bad field since there is no class Widget in this test.
+.field public static widget:LWidget;
+
+.method public constructor <init>()V
+    .registers 2
+    invoke-direct {v1}, Ljava/lang/Object;-><init>()V
+    return-void
+.end method
+
+.method public static constructor <clinit>()V
+    .registers 1
+    new-instance v0, Ljava/lang/Object;
+    invoke-direct {v0}, Ljava/lang/Object;-><init>()V
+    sput-object v0, LBadField;->widget:LWidget;
+    return-void
+.end method
\ No newline at end of file
diff --git a/test/173-missing-field-type/src-art/Main.java b/test/173-missing-field-type/src-art/Main.java
new file mode 100644
index 0000000..61bb918
--- /dev/null
+++ b/test/173-missing-field-type/src-art/Main.java
@@ -0,0 +1,34 @@
+/*
+ * Copyright (C) 2018 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) throws Throwable {
+        try {
+            // Class BadField is defined in BadField.smali.
+            Class<?> c = Class.forName("BadField");
+            System.out.println("Not reached");
+            c.newInstance();
+        } catch (NoClassDefFoundError expected) {
+            // The NoClassDefFoundError is for the field widget in class BadField.
+            if (expected.getMessage().equals("Failed resolution of: LWidget;")) {
+                System.out.println("passed");
+            } else {
+                System.out.println("failed: " + expected.getMessage());
+            }
+        }
+    }
+}
diff --git a/test/173-missing-field-type/src/Main.java b/test/173-missing-field-type/src/Main.java
new file mode 100644
index 0000000..172d6a6
--- /dev/null
+++ b/test/173-missing-field-type/src/Main.java
@@ -0,0 +1,22 @@
+/*
+ * Copyright (C) 2018 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 {
+    // Allow test to pass on RI without adding to knownfailures.json file.
+    public static void main(String args[]) throws Exception {
+        System.out.println("passed");
+    }
+}
diff --git a/test/etc/default-build b/test/etc/default-build
index c61de0a..e06b367 100755
--- a/test/etc/default-build
+++ b/test/etc/default-build
@@ -500,7 +500,10 @@
 if [ "${HAS_SMALI}" = "true" -a ${NEED_DEX} = "true" ]; then
   # Compile Smali classes
   ${SMALI} -JXmx512m assemble ${SMALI_ARGS} --output smali_classes.dex `find smali -name '*.smali'`
-
+  if [[ ! -s smali_classes.dex ]] ; then
+    echo ${SMALI} produced no output. >&2
+    exit 1
+  fi
   # Merge smali files into classes.dex, this takes priority over any jasmin files.
   make_dexmerge classes.dex smali_classes.dex
 fi